Running animation only playing one frame while idle animation works

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

Hello im currently practicing on how to use godot and im working on a simple platformer but my running animation only plays one frame and the running animation plays when i jump heres my code below

extends KinematicBody2D

Defining Variables for movement and gravity

export (int) var gravity = 4000
export (int) var speed = 200
export (int) var jump_speed = -1000

Movement for button pressed

var velocity = Vector2.ZERO
onready var animation = $AnimatedSprite

func _physics_process(delta):
velocity.x = 0

if velocity.y == 0 and velocity.x == 0:
	if is_on_floor():
		animation.play("idle")

if Input.is_action_pressed("move_right"):
	velocity.x = speed
	if is_on_floor():
		animation.play("run_animation")
		animation.flip_h = false

if Input.is_action_pressed("move_left"):
	velocity.x -= speed
	if is_on_floor():
		animation.play("run_animation")
		animation.flip_h = true
	
velocity.y += gravity * delta
velocity = move_and_slide(velocity,Vector2.UP)
if Input.is_action_just_pressed("jump"):
	if is_on_floor():
		velocity.y = jump_speed
:bust_in_silhouette: Reply From: Geazas

You need to check if run_animation is still playing before animation.play("run_animation") again. Otherwise the animation will be re-run every frame causing it to run on only the first frame

if Input.is_action_pressed("move_right") and animation.animation != "run_animation":
velocity.x = speed
if is_on_floor():
    animation.play("run_animation")
    animation.flip_h = false
:bust_in_silhouette: Reply From: Grizly

example:
var export jump_speed = 100
var export speed = 80

if Input.is_action_pressed(“move_right”):
velocity.x = speed
elif Input.is_action_pressed(“move_left”):
velocity.x = -speed

if is_on_floor():
if Input.is_action_just_pressed(“jump”):
velocity.y = -jump_speed

if is_on_floor():
if velocity.x == 0:
animation.play(“idle”)
elif velocity.x != 0:
animation.play(“run_animation”)
elif is_on_floor() == false:
if velocity.y < 0:
animation.play(“jump”)
elif velocity.y > 0:
animation.play(“fall”)

if velocity.x > 0:
animation.flip_h = true
elif velocity.x < 0:
animation.flip_h = false

velocity.y += gravity * delta
velocity = move_and_slide(velocity,Vector2.UP)