0 votes

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)

Godot version 3.4.3
in Engine by (36 points)

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

yes. I connected a signal and put a collision shape

1 Answer

+1 vote
Best answer

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

by (19,272 points)
selected by

I added it but still didn't work

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

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

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.

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?

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... :)

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 Frequently asked questions and 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 [email protected] with your username.