AnimationPlayer only playing when action released

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jjphung
:warning: Old Version Published before Godot 3 was released.

The animation only triggers when I let go of the button and not while it is moving. Can not find a function for is_action_hold or something like that.

extends KinematicBody2D

const GRAVITY = 200
const WALK_SPEED = 250

var velocity = Vector2()
var sprite
var animPlayer

func _ready():
	sprite = get_node("Sprite")
	animPlayer = get_node("AnimationPlayer")
	set_fixed_process(true)

func _fixed_process(delta):
	
	velocity.y += delta * GRAVITY
	if (Input.is_action_pressed("ui_left")):
		velocity.x = -WALK_SPEED
		sprite.set_frame(0)
		animPlayer.play("walk_left")
		
	elif (Input.is_action_pressed("ui_right")):
		velocity.x = WALK_SPEED
		sprite.set_frame(4)
		animPlayer.play("walk_right")
		
	else:
		velocity.x = 0

	var motion = velocity * delta
	motion = move(motion)

	if (is_colliding()):
		var n = get_collision_normal()
		motion = n.slide(motion)
		velocity = n.slide(velocity)
		move(motion)
:bust_in_silhouette: Reply From: Zylann

is_action_pressed does what you want, it stays true as long as the key is pressed. Because you put your code in _fixed_process, it will run every physics frame. Which means you ask Godot to play "walk_left" every 0.016 seconds. Playing an animation starts from its beginning, so it will look like it doesn’t animate because you endlessly stick it at the beginning.

What you can do instead is to check if the animation is playing before, or do this part in _input rather than _fixed_process.