0 votes

My goal is to develop a card game where I can have multiple card objects to manipulate. However, with my current implementation, every time I try to drag just one card, it drags every existing card. I assume what is happening is that the on-click signal is broadcasting to every single one, but I have no idea how to implement this idea without using signals or attaching an ID to each card for the signal to check. Can I process the click event without using signals? If not, how would you suggest implementing/checking card names?

Here is my code:

extends Area2D

var can_grab = false
var grabbed_offset = Vector2()

var enlarged = false


func _on_Card_input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton:
        can_grab = event.pressed
        grabbed_offset = position - get_global_mouse_position()


func _process(delta):
    if Input.is_mouse_button_pressed(BUTTON_LEFT) and can_grab:
        # Increases size when dragging
        if not enlarged:
            self.scale = Vector2(self.scale.x * 1.2, self.scale.y * 1.2)
            enlarged = true
        # Changes position to mouse position
        position = get_global_mouse_position() + grabbed_offset

    if not Input.is_mouse_button_pressed(BUTTON_LEFT):
        # Decreases size when no longer dragging
        if enlarged:
            self.scale = Vector2(self.scale.x / 1.2, self.scale.y / 1.2)
            enlarged = false
Godot version v3.2.3.stable.official
in Engine by (12 points)

1 Answer

0 votes

Hmm, I don't think the issue is using signals. Your code looks like it should work Except you should use global_position instead of position when calculating grabbed_offset.

I assume you are instancing the cards at runtime? Does this happen if you instance the cards in the editor and then run the game?

by (577 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.