I'm trying to create a clickable sprite. Using the answer from https://godotengine.org/qa/50469/implementing-clickable-sprites , I've created a scene with an Area2D, a CollisionShape2D, and an AnimatedSprite. This includes:
Area2D Campfire
- CollisionShape2D
- AnimatedSprite
In Campfire.gd:
signal campfire_clicked
func _input_event(viewport, event, shape_idx):
if event is InputEventMouseButton \
and event.button_index == BUTTON_LEFT \
and event.is_pressed():
emit_signal("campfire_clicked")
func _on_Campfire_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton \
and event.button_index == BUTTON_LEFT \
and event.is_pressed():
emit_signal("campfire_clicked")
In Camp:
CampDisplay
- ColorRect background
- Campfire scene instance
Event campfire_clicked()
is connected to _on_CampDisplay_campfire_clicked()
, which will just print out a message that the campfire was clicked.
When I pull up the camp screen, I can see the campfire, but clicking on it doesn't produce a campfire_clicked
event.
The approach I'm using to change to this 2D scene is a copy of one that I was able to use successfully in another context. A button added to the scene generates on-click events correctly, but the Area2D doesn't.
What have I missed?