Why can't I track a click on a sprite?

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

var position_draw = Vector2(200, 100)

func _ready():

	var a = Sprite.new()
	a.name = "test_sprite"
	a.texture = load("res://img/120px-Roundel_of_El_Salvador.svg.png")
	a.position = position_draw
	self.add_child(a, true)
	
	var coll = CollisionShape2D.new()
	coll.name = "test_collision"
	var circle = CircleShape2D.new()
	circle.radius = 200
	coll.shape = circle
	coll.position = position_draw
	self.add_child_below_node($test_sprite, coll, true)

	print(self.get_children()) #[test_sprite:[Sprite:1267], test_collision [CollisionShape2D:1271]]
	
	print(self.print_tree_pretty())
	#┖╴Area2D
	#    ┠╴test_sprite
	#    ┖╴test_collision
	#Null

	self.connect("input_event", self, "_on_Area2D_input_event")

func _on_Area2D_input_event(viewport, event, shape_idx):
	if InputEventMouseButton and event.is_pressed():
		print("Click")

_on_Area2D_input_event - doesn’t work

:bust_in_silhouette: Reply From: SnapCracklins

I think you asked this question on another forum too? I answered it there but in case you missed it, here it is for who needs it.

you need a variable to toggle on/off to show that your sprite can be clicked. Have that variable change whenever the mouse enters, change your signal to mouse entered (and have a second signal for mouse exited to turn the var off) and whenever the mouse enters, if the bool is flipped, have the click occur. The problem with running a check on the input event is it is only running when an input occurs. You need to make a function that tracks input vs location.

So everything up to that signal is fine. Just a bit of rework to try, you may want to play with the logic to get it right.

    extends Area2D

var position_draw = Vector2(200, 100)
var _can_click = false

func _ready():

	var a = Sprite.new()
	a.name = "test_sprite"
	a.texture = load("res://img/120px-Roundel_of_El_Salvador.svg.png")
	a.position = position_draw
	self.add_child(a, true)
	
	var coll = CollisionShape2D.new()
	coll.name = "test_collision"
	var circle = CircleShape2D.new()
	circle.radius = 200
	coll.shape = circle
	coll.position = position_draw
	self.add_child(coll)
	
	self.connect("mouse_entered", self, "_on_Area2D_mouse_entered")
    self.connect("mouse_exited", selfm "_on_Area_2D_mouse_exited")


func _on_Area2D_mouse_entered():
        _can_click = true


func _on_Area2D_mouse_exited():
        _can_click = false


func _click_check():
       if _can_click:
            if Input.is_action_pressed("click"):
                  print('Click')


func _process(_delta):
       _click_check()

I fixed the typo in your answer:

self.connect("mouse_exited", selfm "_on_Area_2D_mouse_exited")
self.connect("mouse_exited", self, "_on_Area_2D_mouse_exited")

But the code still does not work. As before, there are no mistakes in the console…
Signals are simply not connected.

func _on_Area2D_mouse_entered():
		_can_click = true
		print("ok")


func _on_Area2D_mouse_exited():
		_can_click = false
		print("ok")

Does not work

Incognito | 2022-11-27 03:32