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

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 !!!

in Engine by (15 points)

1 Answer

+1 vote
Best answer

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")
by (3,505 points)
selected by

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

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.

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)

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

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.