How do I check mouse click within the extents of my sprite?

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

I have my button scene as follows:

Sprite
   Area2D
      CollisionShape2D
   Tween
   Sample2D

In code, I am currently using the mouse_enter() and mouse_exit() signals to start different tweens. What I would like to do next is be able to click on my sprite. I tried using the input_event() under the CollisionObject2D but that seems to executes my print message as soon as my mouse enters the sprite’s area. I only want it to execute code after a mouse click. What should I do?

Thanks

:bust_in_silhouette: Reply From: haz

use the input_event parameters so something like this:

func _on_Area2D_input_event( viewport, event, shape_idx ):
if event.type == InputEvent.MOUSE_BUTTON:
	print("mouse clicked the area")
    #tween.blahblah

It doesn’t seem to execute the print code. This is after I attach the signal from the node window:

func _on_Area2D_input_event( viewport, event, shape_idx ):
#print("sprite click on " + my_letter)
if event.type == InputEvent.MOUSE_BUTTON:
	print("click")

edit: I take that back, I haven’t connected the node. Although is it possible to have the code detect when the mouse click is released?

ChewAllTheThings | 2017-08-06 15:48

yes it is possible. try:
if event.type == InputEvent.MOUSE_BUTTON and not event.pressed:
#print

if event pressed that means clicked if not that means released.

haz | 2017-08-09 11:10