Step 9: Maintaining the Animation While Moving

Goal:


Currently, even though all of the code for changing the costume is right, we aren’t done yet.

The shark is still coded to go to a specific sprite when the left and right arrow keys are pressed, so we have to fix that next.

Our costume changes will continue to persist as long as the sadAnimationTime variable is still > 0.

Once we finish this code, our game will now look a lot better and will be ready to play! Notice how we still have an unused shark costume, see if you can find a way to add this to the game as well!

Steps:

  1. Look for the left and right movement code in the Sharky sprite underneath the “when I receive GameStart!” block (it is the last two if statements “if key left arrow pressed” and the “if key right arrow pressed”).
  2. From Control, grab two if then else statements (it’s different from the if then statements!) and place one in each of the last two if statements.
  3. From Operators, grab two > blocks and place one inside each if then else statement.
  4. From Variables, grab two round sadAnimationTime blocks and place them into the left side of the > blocks. Set the right side to the number zero.
  5. From Variables, grab four set nextCostume to blocks and place two inside each if then else blocks. Change the numbers to reflect the costume indexes that we need to change when the Shark is moving left or right.

Code: