How to check a Button node has keeping pressed?

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

I hope to:
When the Button node has keep pressed ,player keep move until i released the button node

:bust_in_silhouette: Reply From: DaveMS

Hi.

I think you could use button_down() and button_up() events, and one or two boolean vars (depending on what you want to achive).

See the example below:

extends Button

var bln_button_is_down = false
var bln_button_has_been_pressed = false

func _process(delta):
    if bln_button_has_been_pressed:
	    if bln_button_is_down:
            print ("Button is pressed")
	    else:
            print ("Button no longer pressed")

func _on_Button_button_down():
	bln_button_is_down = true
	bln_button_has_been_pressed = true

func _on_Button_button_up():
	bln_button_is_down = false

thanks for your answer!

fish2091852127 | 2018-12-31 01:47

:bust_in_silhouette: Reply From: p7f

What i would do is define a velocity variable initializad on zero. On each phisics processing step (i assume player is kinematic body) ill move the player according the velocity. I would connect button_down signal of the button to a function that sets velocity to the value expected in movement, and button_up signal to a fucntion that sets velocity to 0.

Something like this:

extends KinematicBody2D

var velocity = Vector2(0,0)

func _physics_process(delta):
    velocity = move_and_slide(velocity) #assuming top down game here

func _on_Button_button_down():
    velocity =Vector2(100,0) # or whatever you want

func _on_Button_button_up():
    velocity = Vector2(0,0)

This is only ilustrative. With more info perhaps i could help more.

thanks for your answer!

fish2091852127 | 2018-12-31 01:47