Topic was automatically imported from the old Question2Answer platform.
Asked By
toivocat
So I am making a 2D game and I need a walking animation. So I am using an AnimatedSprite and there is just one HUGE problem. So the AnimatedSprite node only playes the animation if the player is pressing right. Here is my code so far:
var test = false
func _process(delta):
if test == true:
$AnimatedSprite.play(“walk”)
if Input.is_action_pressed(“ui_left”):
test = true
if Input.is_action_pressed(“ui_right”):
test = true
if Input.is_action_pressed(“ui_up”):
test = true
if Input.is_action_pressed(“ui_down”):
test = true
else:
test = false
Hello,
This is because your if s are all tested, so in the last one :
if Input.is_action_pressed("ui_down"):
test = true
else:
test = false
the test = false part will run if the ui_down action is not pressed, regardless of other actions.
You should use elif (shorthand for else and if combined) to prevent this:
var test = false
func process(delta):
if test == true:
$AnimatedSprite.play("walk")
if Input.is_action_pressed("ui_left"): # ui_left ?
test = true
elif Input.is_action_pressed("ui_right"): # then, ui_right ?
test = true
elif Input.is_action_pressed("ui_up"): # then, ui_up ?
test = true
elif Input.is_action_pressed("ui_down"): # then, ui_down ?
test = true
else:
# none of them is pressed
test = false