SetAnimation not playing sprite animation in boolean expression

project link: https://studio.code.org/projects/gamelab/qLRfNk7Ahef2sqkznhs4TFQwc5o_tXdOC1_8DYnGDy0

(code is not fully completed yet)
I’m trying to get the animated sprite on the left to play a jumping animation when up, w, or space is pressed. However, when I do press the button, the sprite goes up but when it changes the sprite, it only displays the first frame of the sprite and does not play the rest of the animation. I’ve tried different ways to make the sprite animate, including making the setFrame increase by 1 each frame. I can’t see any way to make the animation play other than making an entirely new sprite and getting rid of the old one.

Hi there @jg9406706,

Regarding your issue with animations it’s a bit tricky and i hope you can wrap your head around what’s happening, On the event a jump key is pressed it sets the current animation of peppino to his jumping self, however the running animation is re invoked at the beginning of the draw loop setting the animation index back to 0 which means when the jump key is still being held down it’ll switch back to the jumping animation again but will never progress past the first frame

Example using a diagram pseudo code:
peppino to run animation; // resets animation each time
if (jump key is being held down) {
peppino to jump animation
}
draw all sprites()

Now that we’ve diagnosed the bug, what can we do?

well first off you don’t need the boolean to tell when the player is airborne I’d suggest avoiding confusion with your users who are reading the code and then incorporate testing player Y velocity for what animation we should be playing so that they don’t interfere with one another, Here’s my snippet on how i solved the issue

if (peppino.velocityY === 0) {
    if (keyWentDown("up") || keyWentDown("w") || keyWentDown("space")) {
      peppino.velocityY = -15;
      peppino.setAnimation("peppinoJump");
    } else {
      peppino.setAnimation("Peppino");
    }
  }

Also as a side note you probably also want the user not to be able to hold the jump key down and just keep jumping when they hit the floor, unless you want that it’s your program after all

Hopefully this answers your question, if not I’m sure others could probably explain this better than I can

All the best, Varrience

1 Like

Thanks a bunch! The code worked, I’ll keep in mind your advice when I advance on the code.