Can a Sprite handle input? How can I know if the player clicked on a Sprite?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ranmaru90
:warning: Old Version Published before Godot 3 was released.
:bust_in_silhouette: Reply From: ranmaru90
  1. Put an Area2D node as a child of the Sprite.
  2. Set it to pickable.
  3. Add a CollisionShape2D or CollisionPolygon2D node to be a child of the Area2D node.
  4. Make sure whichever you use has a proper shape/polygon (select the CollisionShape2D node or the CollisionPolygon2D node, then see the Inspector, set the Shape or build your Polygon).
  5. Then select the Area2D node in your hierarchy, and click on the “Edit connections…” icon/button.
  6. See under “CollisionObject2D”, there’s “input_event”.
  7. Connect that to the node that contains your script and then your Sprite can take inputs and you can type there to do whatever when there’s a mouse click etc.
  8. If you want to handle hover in and hover out, connect the “mouse_enter” and “mouse_exit” too.

It really is the Area2D taking in the input though.

Or, just add a Control and set the Sprite to be its child. It can handle input events too.

Thx for this readable answer, that solved also my problem !

chamawix | 2016-10-07 17:27

Thank you that helped me, the second option is faster and easy.

sanjib | 2017-03-05 19:02

I tried to do everything as you suggested but it doesn’t detect the clicks on the Area. I’ve created an Area2D child of the sprite, then Polygon2D as child of the area and defined the area. Then I select the Area2D and connect it with the _input_event but nothing happens. Am I missing something ?

AlvaroAV | 2017-04-26 07:53

I think it’s because you used Polygon2D.

Polygon2D is not the same as CollisionPolygon2D. Haha. :smiley:

ranmaru90 | 2017-04-26 08:57

I misswrote it, I’m using PolygonCollission2D. Anyway, if this structure should work, I’m doing something wrong. I’ll start from scratch again and see if I can make it work. Thanks for your answer, it’s very good

AlvaroAV | 2017-04-26 09:56