How to detect clicks in an area2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By [woshfja]0h

I am making a Point & Click game, and I am trying to show a sprite when an area2D is clicked (outside of sprite). Then I need it to disappear when another click is registered. Can anyone help me?

:bust_in_silhouette: Reply From: timothybrentwood

Something along these lines should do the trick:

extends Area2D

onready var the_sprite = get_node("path/to/sprite")

func _ready() -> void:
	input_pickable = true
	self.connect("input_event", self, "_on_Area2D_input_event")

func _on_Area2D_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event.is_action_pressed("mouseleft"): # set this up in project settings
		the_sprite.visible = not the_sprite.visible
1 Like