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