How can i get the mouse event in a object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rilson Figueiredo Mi
:warning: Old Version Published before Godot 3 was released.

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

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

sanjib | 2017-03-27 13:53

:bust_in_silhouette: Reply From: sanjib

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

Thanks,

It’s working now.

Rilson Figueiredo Mi | 2017-03-30 16:30

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 Gledhill | 2018-06-28 21:07

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.

ceebecee | 2018-10-13 03:08