Reputation: 239
I have this game where a function spawns random objects every time a timer fires. It look like this:
function showCountDown (event)
-- Condition to show and hide countdown
if countDownNumber == 0 then
spawnShit = 0
timer.cancel( timerSpawn )
timer.cancel(countdownTimer)
print( 'NO MORE SPAAAAAAAAAAAAAAAWWNS' )
end
if countDownNumber >= -1 then
countDownNumber = countDownNumber -1
countDownText.text = countDownNumber
spawnShit = 1
end
if score == nil then
score = 0
end
return true
end
The problem is the timer somehow insists on stopping on -2 instead of on 0, which is quite frustrating. Can anyone see what I'm doing wrong? Maybe you have a better method for creating a countdown timer that triggers different events(stops spawning objects, start win/lose/pause screen etc.
Also it seems like (from looking at the console during testing) that the function showCountDown
is triggered about 3 times extra, after the spawnShit = 0, timer.cancel(myTimers) is fired, which is odd because I'm canceling the timer that triggers the event.
Upvotes: 1
Views: 515
Reputation: 29493
You have
if countDownNumber >= -1 then
countDownNumber = countDownNumber - 1
This means that the if
block will be entered whenever countDownNumber
is larger or equal to -1, i.e. any positive number, 0, and -1. Inside the block, it gets decreased by 1 so the last value will be -2.
You probably want >= 1
, which will make its last value 0:
if countDownNumber >= 1 then
countDownNumber = countDownNumber -1
Upvotes: 1