Problem with timers

Hello. I’m building a 1942 type game and I’m having a problem with the timers. 

function scene:enterScene( event ) startTimers() end function startTimers() firePlayerBulletTimer = timer.performWithDelay(2000, firePlayerBullet, -1) generateIslandTimer = timer.performWithDelay(5000, generateIsland, -1) end function firePlayerBullet() local tempBullet = display.newImage("bulletplayer.png", (player.x+playerWidth/2)-bulletWidth, player.y-bulletHeight) table.insert(playerBullets, tempBullet) planeGroup:insert(tempBullet) end function generateIsland() local tempIsland = display.newImage("island.png", math.random(0, display.contentWidth-islandWidth), -islandHeight) table.insert(islands, tempIsland) islandGroup:insert(tempIsland) print("Island generated") end

When the timer goes off, the firePlayerBullet() and generateIsland() functions get called twice as opposed to once and I’m not sure why it does that

hi @annetawamono

Your 2 timer.performWithDelay have -1 which means will loop forever until you stop them.

Change to 1 to run once or remove them altogether which default as run once.

generateIslandTimer = timer.performWithDelay(5000, generateIsland)

Good Luck!

burhan

an interesting snippet re timers can be found in the spawning tutorial (blog last 2-3 weeks) - i haven’t got round to it yet but mind banked it for later - as it looked like a good solution for tracking timers and stopping the duplicate occurrence of a timer.

T.

Hey @ToeKnee

Is this the tut you were talking about?

http://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/

If it is, can you explain how you stop the duplicating of a timer. 

Thank you

Yes thats it, as  said i haven’t got around to it yet, but lines 34-37 and Brent’s description give the details.

Within the if statement it checks to see if the name of your timer exists, and then B4 starting a new one it cancels the old, and when stopping the timer it cancels it.

It provides a fallback failsafe - if that name has been used somewhere else and is a timer then cancel it.

The tut is about spawning but with lots and lots of these tuts there are bits of code or procedure (used in the tut- that can be utilized in something else) that are really useful in developing your coding skills. AND they happen every week so my advice is check out the blog at least once a week.

T.

hi @annetawamono

Your 2 timer.performWithDelay have -1 which means will loop forever until you stop them.

Change to 1 to run once or remove them altogether which default as run once.

generateIslandTimer = timer.performWithDelay(5000, generateIsland)

Good Luck!

burhan

an interesting snippet re timers can be found in the spawning tutorial (blog last 2-3 weeks) - i haven’t got round to it yet but mind banked it for later - as it looked like a good solution for tracking timers and stopping the duplicate occurrence of a timer.

T.

Hey @ToeKnee

Is this the tut you were talking about?

http://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/

If it is, can you explain how you stop the duplicating of a timer. 

Thank you

Yes thats it, as  said i haven’t got around to it yet, but lines 34-37 and Brent’s description give the details.

Within the if statement it checks to see if the name of your timer exists, and then B4 starting a new one it cancels the old, and when stopping the timer it cancels it.

It provides a fallback failsafe - if that name has been used somewhere else and is a timer then cancel it.

The tut is about spawning but with lots and lots of these tuts there are bits of code or procedure (used in the tut- that can be utilized in something else) that are really useful in developing your coding skills. AND they happen every week so my advice is check out the blog at least once a week.

T.