How to check if cursor in Area2D

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

I know this is super easy to do, but it just doesn’t work??

extends StaticBody2D

func _ready():
	pass

func _on_Area2D_mouse_entered():
	print("mouse entered")
		
func _on_Area2D_mouse_exited():
	print("mouse exted")

I also want to move the object every time the cursor gets in Area2D(So yes, you won’t be able to click the object at all)

You connected a signal and there is a collision shape ,right ?

horsecar123 | 2022-03-30 21:26

yes. I connected a signal and put a collision shape

Cobaltiz | 2022-03-31 11:54

:bust_in_silhouette: Reply From: jgodfrey

I assume you forgot to add a CollisionShape2D or a CollisionPolygon2D as a child of your Area2D node. Without that, it can’t interact with other objects (including the mouse).

I added it but still didn’t work

Cobaltiz | 2022-03-31 11:23

I tested this locally in a new scene by:

  • Add a new Area2D
  • Add a child CollisionShape2D (and define its shape and size)
  • On the Node2D, connect the mouse_entered() and mouse_exited() signals.

This worked as expected and you should be able to prove that to yourself quickly as a test. If it works in a new scene but not in your other scene, I’d guess there’s a Control-type node in the scene tree that’s blocking the mouse input.

If that’s the case, you’ll need to find the offending node and set its mouse_filter property to Ignore.

Also, the Area2D should have non-empty collision layer definition and its pickable property should be set to ON (those should be the default values though).

jgodfrey | 2022-03-31 14:00

Thank you! I had a control node in my scene but I didn’t know it would effect my game. Now, if you have time, could you help me make the object telleport randomly in the scene? I need to make an annoying game where you are supposed to ‘click’ the object but you won’t be able to do so since it would move everytime the cursor is in the Area

Cobaltiz | 2022-03-31 14:20

Something like this should work:

First, create an RNG (for random number generation) and get access to the screen size:

onready var rng = RandomNumberGenerator.new()
onready var screenSize = get_viewport().get_visible_rect().size

Next, randomize the RNG to ensure that you get truly random numbers:

func _ready():
	rng.randomize()

Finally, in your existing mouse_entered event handler, create a new random position for the Area2D node:

func _on_Area2D_mouse_entered():
	var new_x = rng.randi_range(0, screenSize.x)
	var new_y = rng.randi_range(0, screenSize.y)
	$Area2D.position = Vector2(new_x, new_y)

With that, each time you enter the Area2D it should move to a different, random screen position.

jgodfrey | 2022-03-31 18:33

Hi! Thanks but it doesn’t work. I check the position, and only the Area2D node moves. I also want to see the button move(it’s a sprite). What node hould I make the parent?

Cobaltiz | 2022-03-31 19:07

I guess it depends on the details, but generally, you can just make the Area2D the parent and the children all of the other nodes that need to move with it. So, something like this:

Area2D
   CollisionShape2D
   TextureButton # or whatever this control is

And, it does work… :slight_smile:

jgodfrey | 2022-03-31 19:19