This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hi guys,

How can i identify when a object KinematicBody2D receive a mouse click event in a collision area or in a sprite area.

I can identify the mouse event in total scene area but i need the sprite area only.

There is the possibility to identify the mouse click in a object without use collision with mouse position(x,y)?

Thanks

in Engine by (13 points)
edited by

I got the mouse event using RigidBody2d, if you use RigidBody2d you get an option in Inspector tab named "Pickable" click to set it on, then in Node tab under CollisionObject2d text there is input_event(),connect this event to your script. A function will generate automatically like this on your script:

func _on_sprite_input_event( viewport, event, shape_idx ):

1 Answer

+1 vote

I got the mouse event using RigidBody2d, if you use RigidBody2d you get an option in Inspector tab named "Pickable" click to set it on, then in Node tab under CollisionObject2d text there is input_event(),connect this event to your script. A function will generate automatically like this on your script:

func _on_sprite_input_event( viewport, event, shape_idx ):
by (46 points)

Thanks,

It's working now.

I did this exact thing, but kept getting 2 events when I clicked the mouse. I wanted to be able to click on a rigidbody2d to toggle a state on and off, but I get 2 rapid fire clicks.

Any idea about how to differentiate those clicks so I can basically say "when you click the mouse on me do this"?

Judd, It's firing on the "down" and "up" of the click of the mouse button. To differentiate, it looks like you can check the "pressed" property of the event object, which will be true on click and false on release. You can do print(event.as_text( )) to print out the properties of the event to the terminal to see the other info in the object.

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.