0 votes

So, I'm making a card game where I need to click and drag inside a Area2D in order to move the card. The tree node from the card itself consists of:

- Area2D # With a script attached (look below)
   - TextureRect1 # Border texture of the card
       -TextureRect2 # The image that the card has in the center
   - Label # Some labels like name, description and cost
   - Label2
   - Label3
   - CollisionShape2D # The collision shape made exactly the size of the TextureRect1.

With that said, I have connected the Area2D to itself with the Area2D's script beeing:

func _on_Card_mouse_entered() -> void:
if Input.is_mouse_button_pressed(1):
    self.position = get_global_mouse_position()

That's the only thing the whole card has, nothing else, what I can expect is that the mouse_entered only returns true when the mouse get into the border.

Thing is, I also tried attaching the mouseentered & mouseexited signals to the area and made it print a debug message, run the game and when the mouse gets in and out of the collision shape it does not send the debug print, instead, only sends the message when I click inside of the area, no matter if it is left or right. Seems like it needs to update or something. Is very weird and I have looked two times if anything under the collisionshape2D properties were changed but everything seems fine. I thought the order of the nodes caused the wrong behaviour...

If you have ANY idea of what is going on please send a answer, even if you don't really pin point the problem, anything helps. Thanks!

in Engine by (104 points)

1 Answer

+3 votes
Best answer
func _on_Card_mouse_entered() -> void:
    if Input.is_mouse_button_pressed(1):
        self.position = get_global_mouse_position()

This above code will not work because _on_Card_mouse_entered() is called only once when your mouse enters. In only 1 frame you cannot change its position by dragging .

Try this

func _on_Card_mouse_entered() -> void:
    is_mouse_inside = true

func _on_Card_mouse_exited() -> void:
    is_mouse_inside = false

func _process(delta):
    if is_mouse_inside and Input.is_mouse_button_pressed(1):
        selected = true
    if selected and not Input.is_mouse_button_pressed(1):
        selected = false
    if selected:
        position = get_global_mouse_position()
by (731 points)
selected by

Yeah, I tried doing that too, but the problem persists, I wrote this:

func _on_Card_mouse_entered() -> void:
print("Mouse IN")
is_mouse_inside = true

func onCardmouseexited() -> void:
print("Mouse OUT")
ismouseinside = false

func process(delta):
if is
mouseinside and Input.ismousebuttonpressed(1):
self.position = getglobalmouse_position()

But the prints only execute when I click left or right inside of the Area2D, so:
Run the game -> create the card(instanced as the tree mentioned) -> try to drag(doesn't work) -> drag clicking from the outside of the area and release click inside of the Area2D(The console output sends IN and OUT prints instantly after the release on the drag+click inside of the area)

No it works fine i have tried it
try my scene https://gofile.io/?c=82K5Gw

(just noticed this si the old question) I made a new question as the problem might envolve a bug, try adding a textureRect the same size as the collisionshape and try to move it aroun with the area2d.

I copied your code, re-attached the signals and added the 2 variables and still is does not work, I know that the onCardmouseentered() only executes when the mouse goes in, but in my first attemp I already tried to fix that, the problem persists as the TextureRect still interfere with the collisionshape or Area2D

@supper_raptor your scenes doesn't have a textureRect inside, that is what is causing the problem, with sprites everything works fine, but texturerect doesn't.

You are right it does not work on textureRect
To make it work you need to set mouse filter to ignore

Here's the pic https://imgur.com/PlgcBgB

Upvote if it works !

Yeah it worked, thanks! Didn't know that mean ignore the mouse xD

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.