Troubles with count down timer

When my timer runs down to 0 the game ends and goes to the restart screen. then when you click restart you can play the game again.  the game runs fine if you do this once, but if you do it more than once the count down timer goes negative and doesn’t stop. any suggestions? this is what i got:

[lua]local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
storyboard.purgeAll()
storyboard.gotoScene(“restart”)
end
end
timer.performWithDelay(1000,timerDown,-1)

function shoot(event)
if event.phase == “began” then
bullsEye.x = math.random(0, display.contentWidth - 40)
bullsEye.y = math.random(0, display.contentHeight - 100)
score = score + 1
count = count + 1
scoreText.text = score
if count == 15 then
timeLimit = timeLimit + 5
count = 0
end
end
return true
end

function endGame(event)
if event.phase == “began” then
bullsEye.alpha = 0
timeLimit = nil
storyboard.gotoScene(“restart”)
end
return true
end[/lua]

Hello ssccard

Are you intializing your orignal value of your timer when

if(timeLimit==0)then

timeLimit=20;

storyboard.purgeAll()

storyboard.gotoScene(“restart”)

end

as i think this is what you are lacking off. try this it may helps!!!

Thanks 

Neel

It’s probably a good policy when writing a chunk of code to have a ‘start’ and ‘end’ pair of functions. The start bit sets up listeners, initialises variables, creates timers etc. The end bit does the opposite, and nils references that aren’t used any more.

It makes it much easier to figure out these sorts of bugs and reduces the chances of memory leakage.

Nilstar141, 

Yes sorry I forgot to include that when I posted my first post. I do already have the following in my code:

[lua]local timeLimit = 20
local count = 0
timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)
timeLeft.x = display.contentCenterX
timeLeft.y = display.contentCenterY
timeLeft:setFillColor(0,0,0)[/lua]

Hi ssccard

can you post some of more code related this so we can better understand…?? and help you out 

and one more thing you are forgetting that when timeLimit reaches at 0 you should do this

timer.cancel(timer name) otherwise that thread will keep going on and on…

if(timeLimit==0)then

 

 

timer.cancel(Your timer name);

 

timeLimit=20;

 

storyboard.purgeAll()

 

storyboard.gotoScene(“restart”)

 

end

 

I think this may helps

Thanks

Neel

Hello ssccard

Are you intializing your orignal value of your timer when

if(timeLimit==0)then

timeLimit=20;

storyboard.purgeAll()

storyboard.gotoScene(“restart”)

end

as i think this is what you are lacking off. try this it may helps!!!

Thanks 

Neel

It’s probably a good policy when writing a chunk of code to have a ‘start’ and ‘end’ pair of functions. The start bit sets up listeners, initialises variables, creates timers etc. The end bit does the opposite, and nils references that aren’t used any more.

It makes it much easier to figure out these sorts of bugs and reduces the chances of memory leakage.

Nilstar141, 

Yes sorry I forgot to include that when I posted my first post. I do already have the following in my code:

[lua]local timeLimit = 20
local count = 0
timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)
timeLeft.x = display.contentCenterX
timeLeft.y = display.contentCenterY
timeLeft:setFillColor(0,0,0)[/lua]

Hi ssccard

can you post some of more code related this so we can better understand…?? and help you out 

and one more thing you are forgetting that when timeLimit reaches at 0 you should do this

timer.cancel(timer name) otherwise that thread will keep going on and on…

if(timeLimit==0)then

 

 

timer.cancel(Your timer name);

 

timeLimit=20;

 

storyboard.purgeAll()

 

storyboard.gotoScene(“restart”)

 

end

 

I think this may helps

Thanks

Neel