Hello,
I’m facing problem in decrementing the incremented timer.
In our game, we have started a timer (i.e 30 seconds)and it goes on decrementing on each second.
and we have a bonus time 5 seconds which is falling randomly from top of screen. On touching the bonus, the timer has to increment 5 seconds and it should continue decrementing.
In our code the timer is working fine,when we don’t collect the bonus.
But when we collect the bonus time, its not working.The bonus time is getting added to the main timer and is decrementing upto a certain time and is getting stuck.
Plot:Suppose the timer starts at 00.30 seconds and keeps on decrementing 00.29, 00.28, 00.27 and so on. after collection the bonus time(i.e 5 seconds) it’s becoming 00.32 and decrementing upto a certain time(i.e 00.05) which is the bonus, and getting stuck.I want it to decrement it to zero and then time over.
Given below is the code, please help us to rectify it:
local finalsecondsLeft=1* 30 – 1 minutes * 60 seconds
local clockText = display.newText(“0:30”, display.contentCenterX, 100, native.systemFontBold, 60)
clockText:setFillColor( 0.7, 0.7, 1 )
local function updateTime()
–print(“function called”)
– decrement the number of seconds
finalsecondsLeft = finalsecondsLeft - 1
– time is tracked in seconds. We need to convert it to minutes and seconds
local minutes = math.floor( finalsecondsLeft / 60 )
local seconds = finalsecondsLeft % 60
– make it a string using string format.
local timeDisplay = string.format( “%02d:%02d”, minutes, seconds )
clockText.text = timeDisplay
if(finalsecondsLeft==0) then
gameover()
end
end
function randomfall()
rand = math.random( 100 )
if (rand < 80) then
j = display.newText("+5s", display.contentCenterX, 100, native.systemFontBold, 40)
clockText:setFillColor( 1, 0.7, 1 )
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
--apple.name = “apple”
function onTouch1(event)
if event.phase==“ended” then
event.target:removeSelf()
finalsecondsLeft=finalsecondsLeft+5
update()
print(“function called”)
audio.play(touchsound2)
end
end
j:addEventListener(“touch”,onTouch1)
end
end
It will be very kind if anyone can help me to solve this problem
Shristi