#Button out of an Area2D

3 messages · Page 1 of 1 (latest)

vale robin
#

Hello, how would I create a button out of an area2D? Here's what I have so far:

@onready var b1 = $character/button

func _ready():
    b1.mouse_entered.connect(_on_b1_pressed)

func _on_b1_pressed():
        print("here")
    if (Input.is_action_just_pressed("ui_click")):
        print("clicked")

"ui_click" is an input map I created for a left click. Code isn't working right now, though. However, the "here" is getting logged to the console whenever I enter the area defined by the collisionShape2D attached to the area2D, so I'm not really sure why the next line doesn't trigger when I click.

Can anyone help me out, I'm not understanding how to do this :/

idle yew
#

Because the function _on_b1_pressed is called the frame your mouse enters, and then not again. So unless you can click within 1/60th of a second after your mouse enters, it will have already checked for a click, failed to find it, and stopped checking.

#

What you need to do is create a is_hovered variable, set it to true when the mouse enters and false when the mouse exits, then use the standard _input function to check for a click, but also checking that is_hovered is true.