Can you have multiple draw loops in a program

I have a student making a game in the Javascript class. He wants the user to click between two buttons to either play against the computer or against each other. He is coding the against AI first. When it loads he wants to have a sprite move randomly but it just puts in a single place because it is only seeing the random 1 time. He wants to be able to put another draw loop so it moves the sprite continuously. It doesn’t seem to like the other draw loop being used. Is there a solution for this or can this not be done?

Hi @matthew.fundak,

You are correct in that there should only be one draw loop. I’m not exactly sure how your student is generating the random number but my guess is he has a variable outside the draw loop generating the random number then uses that in the draw loop? Regardless, if we remember that the draw loop “loops” every 30 seconds or so, then a new random number needs to be generated in the draw loop. Here is a project from one of my students that is a good use of random number to generate moving stars (I think - maybe snow;). You can view the code by clicking VIEW CODE. The random number is in the y position for the small ellipses so that approximately 30 times per second - they are moved up and down the screen.

I hope this helps but if not, please let us know. Also, please share your student’s project so we have a better idea of how to help. You can do this by clicking on the blue SHARE button at the top of the project and then clicking COPY LINK TO PROJECT.

Good luck!
~Michelle