Area2D not detecting mouse in/out properly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaximoTG98

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 mouse_entered & mouse_exited 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!

:bust_in_silhouette: Reply From: supper_raptor
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()

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 _on_Card_mouse_exited() → void:
print(“Mouse OUT”)
is_mouse_inside = false

func _process(delta):
if is_mouse_inside and Input.is_mouse_button_pressed(1):
self.position = get_global_mouse_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)

MaximoTG98 | 2020-04-14 10:10

No it works fine i have tried it
try my scene

supper_raptor | 2020-04-14 17:51

(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 _on_Card_mouse_entered() 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

MaximoTG98 | 2020-04-14 18:15

@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.

MaximoTG98 | 2020-04-14 18:16

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

Upvote if it works !

supper_raptor | 2020-04-14 18:52

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

MaximoTG98 | 2020-04-14 20:29