This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I'm still a bit new to Godot and programing in general and I'm trying to make a 2D game where you can briefly float after jumping, but for some reason whenever I have my player character jump, only the fall animation plays instead of waiting for the jump animation to finish. And when get my character to float only the first frame of the float animation plays.
Here's the portion of code that I set up:


if Input.is_action_pressed("jump") and is_on_floor():
     velocity.y = JUMPFORCE
     $AnimatedSprite.play("Jump")
     not $AnimatedSprite.play("Fall")

velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.2)

if not Input.is_action_pressed("jump") and not is_on_floor():
      $AnimatedSprite.play("Fall")
velocity.y = velocity.y + FALL
if Input.is_action_pressed("Up") and not is_on_floor():
      $AnimatedSprite.play("Float")
      velocity.y = FLOATFALL
Godot version v3.5.1
in Engine by (15 points)

1 Answer

+1 vote
Best answer

AnimatedSprite won't wait for the current animation to finish if you call playbefore that.

It seems you have a mistake in the first if statement, where you play two animations at once:

 not $AnimatedSprite.play("Fall")

This line runs the play method, gets the return value (which is void, so the value will be null) and then tries to use the not boolean operator on it, which results in the value true, which is then discarded, as you don't use it for anything.

Removing the line will partially fix the problem, as now the "Jump" animation runs until the player lets go of the jump button.

You'll need to keep track of the animation playback yourself using the frame and animation properties of AnimatedSprite. You can check which animation is running, and whether it's at the last frame.

One way could be to create a method for checking whether the current frame number is the same as the last frame of the animation:

func has_animation_ended(anim_name):
    if $AnimatedSprite.animation != anim_name:
        # The current animation is named something else
        return true
    # Get the last frame number by decrementing one from the count
    var last_frame_num = $AnimatedSprite.frames.get_frame_count(anim_name)-1
    return $AnimatedSprite.frame == last_frame_num

Then you can just use that whenever you need to make sure a previous animation has ended:

if has_animation_ended("Jump"):
    play("Next Animation")

Just make sure to disable the "Loop" toggle on the "Jump" animation in the SpriteFrames asset, otherwise the jump animation might start over after reaching the last frame.

Alternatively, you could avoid tracking the frame numbers, and instead play the "Jump" animation, and let it hold at the last frame until the player starts falling (after the jump apex). You could do this by playing the "Fall" animation only if the velocity.y property is positive, and the player is not on ground.

You could restructure the code like this:

if Input.is_action_pressed("jump") and is_on_floor():
    velocity.y = JUMPFORCE
    $AnimatedSprite.play("Jump")


velocity.y = velocity.y + FALL
if Input.is_action_pressed("Up") and not is_on_floor():
    $AnimatedSprite.play("Float")
    velocity.y = FLOATFALL
elif velocity.y > 0 and not is_on_floor():
    $AnimatedSprite.play("Fall")

velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.2)
by (509 points)
selected by

Thank you so much, it worked! I was so frustrated trying to figure it out but, you made it look like a piece of cake.

Glad I could help! The way the code is currently structured works for simpler projects, but when it starts to feel you have to keep track of too many states and variables to compare, a popular solution is to use a state machine code pattern. There are also Godot specific tutorials on Official Godot Docs and GDQuest for example. An easier one to implement can also be found on Godot Tutorials

Thanks again! Theses tutorials will definitely be useful.

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.