How would i tell whether a mouse click input is inside an Area 2d ????

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By javrocks

extends Area2D

var touch_down := false

func _ready():
connect(“input_event”, self, “_on_input_event”)

func _on_input_event(viewport, event, shape_idx) → void:

if event.is_action_pressed("ui_touch"):
	touch_down = true
else: touch_down = false

print (touch_down)
:bust_in_silhouette: Reply From: PrecisionRender

The signal input_event(viewport, event, shape_idx) built into Area2Ds is good for getting single presses, in addition to mouse movement, but in your case where you are checking the button state every frame, I haven’t found a good way to use this signal.

Instead, we can use the mouse_entered() and mouse_exited() signals to see if our mouse is in the Area2D before checking if our mouse is being pressed.

Connect your Area2D’s mouse_entered() and mouse_exited() signals to your script, then create a new variable to store if the mouse is inside the Area2D. Your code should look something like this afterwards:

var touch_down = false
var mouse_inside_area = false

func _process(delta):
    if Input.is_action_pressed("ui_touch") and mouse_inside_area:
	    touch_down = true
    else:
	    touch_down = false

    print(touch_down)

func _on_Area2D_mouse_entered():
    mouse_inside_area = true

func _on_Area2D_mouse_exited():
    mouse_inside_area = false

When i run the game it just prints that touch_down is False
even though I’m clicking/entering inside the Area2D’s collision shape

javrocks | 2021-08-31 03:09

Did you connect the signals? Also, in your Area2D, you need to turn on the setting input_pickable so the Area2D will detect for the mouse.

PrecisionRender | 2021-08-31 14:58

Ok its working now thx alot

javrocks | 2021-08-31 17:13