Search code examples
animationmit-scratch

Scratch walking animation broken


What it's supposed to do:

I'm making a game on Scratch in which the character has a walking animation. The walking animation is supposed to switch their costume to a walking stance, wait 0.5 seconds then go to an idle stance, wait 0.5 seconds then go to a walking stance, and repeat that until they let go of the button.

The image below is an image of the animation blocks put together that play the animation when walking left.

Animation blocks.

What it does do and the suspected reason for the bug:

It worked perfectly until I added a block function for the same character when suddenly the animations broke for that character and the other one (who also has a walking animation with the same code). The second image below is an image of the blocking code.
After adding the block function, instead of waiting 0.5 seconds in between costume switches, it seems to switch to the walking stance and then immediately switch to the idle stance instead of waiting.
The image below shows the blocking blocks and the movement it controls. While they are blocking it isn't supposed to be able to move. That part works fine but I included it in case it has something else to do with my problem.

Movement and blocking blocks.

What I have tried:

I tried running the piece of code shown individually (separate from the other animation parts) but the bug still occurs and the animation doesn't wait 0.5 seconds. I also tried changing the other character's animation so that it wouldn't activate when the animation broadcast was sent but it still bugged, which means:

  • The problem isn't because it runs with another sprite too. (Because if it was it would have been fixed when I deactivated the other animation).
  • The problem also isn't because it's connected with the right-side walking and idle animations (Because if it was it would have fixed when I ran it alone).

  • Link to shared game:

    https://scratch.mit.edu/projects/690164519/


    Solution

  • I think the issue is with this part of your code. The forever loop is always trying to set the costume to the idle state when BlockStatus is NonBlocking.

    This is then fighting with the loop that is trying to manage the swap between the idle and the walking costumes.

    You may need to set a flag when the key is pressed and move the walking animation into this loop?

    enter image description here