The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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

in Engine by (59 points)

2 Answers

+1 vote
Best answer

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
by (78 points)
selected by

thanks for your answer!

0 votes

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.

by (3,505 points)

thanks for your answer!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.