Student wants sprite to animate once without any user input

My student wants his sprite to animate without any user input and then stop after one loop if animation. Any idea how this can work?


Can you maybe share a link to the project? I’m not quite sure what you mean by “animate.” Is it an animated sprite (ie has more than one image embedded in it)?

Let us know.



Here is the link!

The student wants the Slash and Pow sprites to only move once and then stop moving.

Hi @hcooper,

What a great game! I am going to make some assumptions. I am going to assume by animate you mean the frame animation of the sprite. So if we break down the problem, it would look like:

  1. After one loop…
    This requires a timer. The draw loop makes it easy to setup a timer. You simply create a variable that starts at 0 and counts each time through the loop. To convert to seconds, it is just a little math. The draw loop “loops” 30x per second so there is some division required;)
  2. Stop animating
    This would be an if statement that checked his desired time from his timer. He can choose the frame of animation to stop on using sprite.setFrame(0). There are many fun things to do with the animation frames in this help page.

Hope these tips help. Let us know if you have any other questions.

Thank you Michelle.

Working with the student, we are not quite sure how the timer needs to be set up. This is what the student has tried.

Yes. I see slashTimer was declared and set = 0. Nice job. Now he just needs to add a counter pattern in the draw loop and the bit of math to convert to seconds:
slashTimer = slashTimer+1/30


1 Like

Thank you for your help!

If you don’t mind, we have a new question.
In the top left there is a health bar, there is already code in place to change the bar’s sprite as if health were being depleted (by a quarter of the bar each time) from previous experimentation with changing the health bars, all of them get activated because of their statement conditions, or none at all. Right now only going to 3/4 of the bar works. It should happen when the space bar is pressed, the function “doraHPTimer” contains the four functions for the top left health bar (for organization) doraHPTimer4(); is for full health, 3 for 3/4, etc., and even though it says it is a timer, it’s not counting down or up, it’s just a name.

This is such a great game. I love the graphics. It is such a big game and there are so many questions I have that without having the student to answer them, debugging is difficult. Here are some tools I use with students to help debug especially the more complex games.

  • Have the student follow one scenario all the way through the code. For example, if the user chooses the mountain dew, what is supposed to happen first, second, third, and then follow those actions line by line in the code making sure everything is there. Often, this process will help reveal the bug.
  • Use the “Watch” tool. You can enter more than one variable or item to watch. It is an invaluable tool especially when so many if statements are being used. It will help you figure out if all the conditions are truly being triggered.
  • Use console.log and have it print something to the console when student feels it should be triggered. If it isn’t being triggered, retrace code to figure out why.

Good luck!
It is a great game