My AnimatedSprite just play the first frame :(

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

Hello everyone,
I’m a beginner. I have a problem with AnimatedSprite.
This is my code:

extends KinematicBody2D

var velocity = Vector2.ZERO

const SPEED = 180
const GRAVITY = 35
const JUMPFORCE = -1000

func _physics_process(_delta):
	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED
		$AnimatedSprite.play("run")
		$AnimatedSprite.flip_h = false
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -SPEED
		$AnimatedSprite.play("run")
		$AnimatedSprite.flip_h = true
	else:
		$AnimatedSprite.play("idle")
	
	if not is_on_floor():
		$AnimatedSprite.play("jump")
	
	velocity.y += 35
	
	if Input.is_action_pressed("ui_up") and is_on_floor():
		velocity.y = JUMPFORCE
	
	velocity = move_and_slide(velocity, Vector2.UP)
	velocity.x = lerp(velocity.x, 0, 0.2)

The jump animation just play first frame, I don’t know how to fix it.
Can you help me. please?
Thank you !!!

:bust_in_silhouette: Reply From: p7f

The problem is that each frame you are changing the animation to either run or idle, and then you change it to jump. As you changed it previously, jump animation starts over every frame. Look at this:

if Input.is_action_pressed("ui_right"):
    velocity.x = SPEED
    $AnimatedSprite.play("run")
    $AnimatedSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    $AnimatedSprite.play("run")
    $AnimatedSprite.flip_h = true
else:
    $AnimatedSprite.play("idle")

After this piece of code, animation will be either “run” or “idle”, as it will always enter one of those three cases. After that you enter this case:

if not is_on_floor():
    $AnimatedSprite.play("jump")

And “jump” starts from the beginning.
You could try something like this instead:

if Input.is_action_pressed("ui_right"):
    velocity.x = SPEED
    $AnimatedSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    $AnimatedSprite.flip_h = true


if not is_on_floor():
    $AnimatedSprite.play("jump")
elif abs(velocity.x) == SPEED:
    $AnimatedSprite.play("run")
else:
    $AnimatedSprite.play("idle")

I tried and succeeded. Thanks.
But I see someone do like me and have no problem

J.Delta | 2020-08-28 14:33

Hi! can you share that other code? if it is just like yours, it should have the same problem. He must be doing somemthing different. Because the logic of your code is as i explained, if im not mistaken or overseeing anything.

p7f | 2020-08-28 14:37

I watch that tutorial video again.
His jump animation just has 1 frame. If it has more, it will have problem.
Now, I’m understand.
Thank you so much!!!
(My English is too bad, hope you sympathize)

J.Delta | 2020-08-28 14:58

Glad to help! If this works to you, you may select the answer so others can see its solved.

p7f | 2020-08-28 15:05