Not being able to click a sprite?

Godot Version

4.6

Question

Hello!! I have an area, parented to an animatedsprite2d, parented to a staticbody2d (although im not sure if that matters but its better to be safe than sorry). I wanted to make this sprite clickable, so i put this into the child area2d’s script:


func _on_area_2d_mouse_entered() -> void:
	print("mouse_entered")
	if Input.is_action_just_pressed("clicked"):
		print("mouse_clicked")



THe mouse_clicked is in my input map, and when my mouse entered, it is printing. It just isnt saying i clicked it. How do i make it clickable, like a button? DO i have to make it a button?

You only check if clicking while entering. Not at other times. You need it in a separately called part of the code. Right now you have to enter and be clicking at the same frame fir it to work.

1 Like

What you need to do is move the mouse clicking code somewhere else and monitor when it’s clickable.

var is_clickable: bool  = false


func _ready() -> void:
	area_entered.connect(_on_slot_entered)
	area_exited.connect(_on_slot_exited)


func _on_mouse_entered() -> void:
	is_clickable = true


func _on_mouse_exited() -> void:
	is_clickable = false


func _process(_delta: float) -> void:
	if is_clickable:
		if Input.is_action_just_pressed("click"):
			print("Clicked")
2 Likes

omg thank you! Ive already learned this lesson once lol i need to sleep /lh

but seriously thank you!! I thought i would need to do another four-day long brainstorm to get it working :sob:

1 Like

There’s an example on how you can do it using Sprite2D.get_rect().