# Timing Variables

Hi!
I have a student who is using Code.org to code his own game for a passion project. He would like to create a timer, that when it reaches 100 a sprite will deploy another sprite and then the timer will reset.

Any suggestions on how I can help him accomplish this?

Thank you!

A basic timer can be created fairly easily in GameLab because it loops 32 times per second.

• Begin with a variable outside the drawloop that is set to 0.
• Use the counter pattern in the draw loop to increase the variable by 1 each time through the loop.
• This alone would cause the variable to reach 100 in about 3 seconds sooooooo…divide it by 32 and you have a timer that counts approximately 1 second each time through the loop.
• Use an if statement that watches the variable and when the timer is greater than or equal to 100, the sprite moves into the scene (it should start off the screen…say x = 500). The if statement can then also reset the timer variable = 0;

Good luck!
~Michelle

While I agree with that there is a bit of an easier way than having to sync the counter with the frame rate especially if it’s going to be performance inducing the fps may not always stay constant as well which means you’ll have to account for delta scaling which I’m guessing no one wants to do more than they have to get a simple timer variable running so may i also suggest my own proof of concept of using setInterval

``````var alarm = 0;
// setInterval(callback, milliseconds)
// @returns Loop ID
var loop = setInterval(function() {
alarm += 1;
} 1000)
``````

by doing this you’ll have control over the running loop and stopping it whenever you need to without any extra calculations… while i can’t deny learning about delta scaling is pretty useful though perhaps show these examples @laurieann.lawson and see which one they’d prefer to use!

Also if my understanding is correct it should be about 33 times given that 1000/33 is closer to 30fps than 32 times. I’m curious though if it is actually 32 though