timer increment

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

Please use code tags (blue < > in menu) when posting code:

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

One issue I see is that in your onTouch1 function you are calling “update()”, however your function above is called “updateTime()”

Thank you for the rectification.

We even called updateTime. Still it’s not working.

How are you doing the per-second timer updates? This sounds consistent with something like

timer.performWithDelay(1000, updateTime, 30) -- hardcoded limit

I tried with that too.I guess I’m not able to convey my doubt properly.In this case I’m sending the entire code.

I hope this may work.

function homescreen()

homebkg=display.newImage(“ct2.jpg”,140,295)

play=display.newText(“Tap to Start”, display.contentCenterX, 230, native.systemFontBold,30)

play:setFillColor(0, 0,.77 )

gamename=display.newText(“TAP”, display.contentCenterX, 5, “Ravie”,40)

gamename:setFillColor(.9, .2,0.2)

play:addEventListener(“touch”,homeend)

end

function homeend()

play:removeEventListener(“touch”,homeend)

local highscore

local myscore=0

local tapsound=audio.loadSound(“tap.wav”)

local touchsound1=audio.loadSound(“Beep3.wav”)

local touchsound2=audio.loadSound(“time.mp3”)

local gameoversound=audio.loadSound(“gameover.mp3”)

local countdowntxt=display.newText(“GAME OVER”, display.contentCenterX, 100, native.systemFontBold, 60)

countdowntxt:setFillColor( 0.7, 0.7, 1 )

local director=display.newImage(“ct.jpg”)

local background = display.newImage( “ct.jpg”)

local physics = require(“physics”)

physics.start()

local widget = require(“widget”)

local function handleButtonEvent( event )

local phase=event.phase

if “ended”== phase then

print( “you pressed and released the button”)

   end

end

local myButton=widget.newButton

{

   left = 100,

   top= 330,

   width = 120,

   height = 120,

   defaultFile = “green.png”,

   overFile = “red3.png”,

   onEvent = handleButtonEvent,

}

local text = display.newText("Your Score: ", 0, 0, native.systemFont, 18)

text.x=150;text.y=50

local function onTap(event)

audio.play(tapsound)

text.text="Your Score: "…myscore

    myscore= myscore+1

print("Your Score: "…myscore)

return true

end

myButton:addEventListener(“tap”,onTap)

audio.play(tapsound)

–code for timer

–display.setDefault(“background”, 0.2, 0.2, 0.4 )

– Keep track of time in seconds

secondsLeft =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()

– decrement the number of seconds

secondsLeft = secondsLeft - 1

– time is tracked in seconds.  We need to convert it to minutes and seconds

local minutes = math.floor( secondsLeft / 60 )

local seconds = secondsLeft % 60

– make it a string using string format.  

local timeDisplay = string.format( “%02d:%02d”, minutes, seconds )

clockText.text = timeDisplay

if(secondsLeft==0) then

gameover()

end

end

– run them timer

local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

–code for random fall

function randomfall()    

rand = math.random( 100 )

if (rand < 40) then

    j = display.newImage(“coin2.png”);

    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 onTouch(event)

    if event.phase==“ended” then

    text.text="Your Score: "…myscore

    myscore= myscore+5

    print(“this”)

    event.target:removeSelf()

    print("Your Score: "…myscore)

    audio.play(touchsound1)

end

print(myscore)

end

j:addEventListener(“touch”,onTouch)

   

elseif (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} )

    function onTouch(event)

    if event.phase==“ended” then

    secondsLeft=secondsLeft+5

    updateTime()

    event.target:removeSelf()

    audio.play(touchsound2)

end

end

j:addEventListener(“touch”,onTouch)

else

    apple = display.newImage(“danger.png”);

    apple.x = 60 + math.random( 160 )

    apple.y = -100

    physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )

    function onTouch(event)

if event.phase==“ended” then

gameover()

end

end

apple:addEventListener(“touch”,onTouch)

end 

end

local dropApples = timer.performWithDelay( 3000, randomfall, -1 )

–code for gameover

function gameover()

local gobck=display.newImage(“homescreen.png”)

local gameovertext=display.newText(“GAME OVER”, display.contentCenterX, 60, native.systemFontBold, 40)

gameovertext:setFillColor( 0.6, 0.6, 1 )

local myscoretext=display.newText(“Your SCORE=”…myscore,display.contentCenterX,140,native.systemFontBold,30)

audio.play(gameoversound)

end

end

homescreen()

Hi. I guess I should have been more specific. The problem, rather than the solution, seems consistent with that sort of thing. 

Indeed, this looks like the culprit:

-- run them timer local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

That won’t pick up on your changes.

Probably the quickest option would be to make that timer endless as well, then cancel both of them in gameover().

Please use code tags (blue < > in menu) when posting code:

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

One issue I see is that in your onTouch1 function you are calling “update()”, however your function above is called “updateTime()”

Thank you for the rectification.

We even called updateTime. Still it’s not working.

How are you doing the per-second timer updates? This sounds consistent with something like

timer.performWithDelay(1000, updateTime, 30) -- hardcoded limit

I tried with that too.I guess I’m not able to convey my doubt properly.In this case I’m sending the entire code.

I hope this may work.

function homescreen()

homebkg=display.newImage(“ct2.jpg”,140,295)

play=display.newText(“Tap to Start”, display.contentCenterX, 230, native.systemFontBold,30)

play:setFillColor(0, 0,.77 )

gamename=display.newText(“TAP”, display.contentCenterX, 5, “Ravie”,40)

gamename:setFillColor(.9, .2,0.2)

play:addEventListener(“touch”,homeend)

end

function homeend()

play:removeEventListener(“touch”,homeend)

local highscore

local myscore=0

local tapsound=audio.loadSound(“tap.wav”)

local touchsound1=audio.loadSound(“Beep3.wav”)

local touchsound2=audio.loadSound(“time.mp3”)

local gameoversound=audio.loadSound(“gameover.mp3”)

local countdowntxt=display.newText(“GAME OVER”, display.contentCenterX, 100, native.systemFontBold, 60)

countdowntxt:setFillColor( 0.7, 0.7, 1 )

local director=display.newImage(“ct.jpg”)

local background = display.newImage( “ct.jpg”)

local physics = require(“physics”)

physics.start()

local widget = require(“widget”)

local function handleButtonEvent( event )

local phase=event.phase

if “ended”== phase then

print( “you pressed and released the button”)

   end

end

local myButton=widget.newButton

{

   left = 100,

   top= 330,

   width = 120,

   height = 120,

   defaultFile = “green.png”,

   overFile = “red3.png”,

   onEvent = handleButtonEvent,

}

local text = display.newText("Your Score: ", 0, 0, native.systemFont, 18)

text.x=150;text.y=50

local function onTap(event)

audio.play(tapsound)

text.text="Your Score: "…myscore

    myscore= myscore+1

print("Your Score: "…myscore)

return true

end

myButton:addEventListener(“tap”,onTap)

audio.play(tapsound)

–code for timer

–display.setDefault(“background”, 0.2, 0.2, 0.4 )

– Keep track of time in seconds

secondsLeft =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()

– decrement the number of seconds

secondsLeft = secondsLeft - 1

– time is tracked in seconds.  We need to convert it to minutes and seconds

local minutes = math.floor( secondsLeft / 60 )

local seconds = secondsLeft % 60

– make it a string using string format.  

local timeDisplay = string.format( “%02d:%02d”, minutes, seconds )

clockText.text = timeDisplay

if(secondsLeft==0) then

gameover()

end

end

– run them timer

local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

–code for random fall

function randomfall()    

rand = math.random( 100 )

if (rand < 40) then

    j = display.newImage(“coin2.png”);

    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 onTouch(event)

    if event.phase==“ended” then

    text.text="Your Score: "…myscore

    myscore= myscore+5

    print(“this”)

    event.target:removeSelf()

    print("Your Score: "…myscore)

    audio.play(touchsound1)

end

print(myscore)

end

j:addEventListener(“touch”,onTouch)

   

elseif (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} )

    function onTouch(event)

    if event.phase==“ended” then

    secondsLeft=secondsLeft+5

    updateTime()

    event.target:removeSelf()

    audio.play(touchsound2)

end

end

j:addEventListener(“touch”,onTouch)

else

    apple = display.newImage(“danger.png”);

    apple.x = 60 + math.random( 160 )

    apple.y = -100

    physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )

    function onTouch(event)

if event.phase==“ended” then

gameover()

end

end

apple:addEventListener(“touch”,onTouch)

end 

end

local dropApples = timer.performWithDelay( 3000, randomfall, -1 )

–code for gameover

function gameover()

local gobck=display.newImage(“homescreen.png”)

local gameovertext=display.newText(“GAME OVER”, display.contentCenterX, 60, native.systemFontBold, 40)

gameovertext:setFillColor( 0.6, 0.6, 1 )

local myscoretext=display.newText(“Your SCORE=”…myscore,display.contentCenterX,140,native.systemFontBold,30)

audio.play(gameoversound)

end

end

homescreen()

Hi. I guess I should have been more specific. The problem, rather than the solution, seems consistent with that sort of thing. 

Indeed, this looks like the culprit:

-- run them timer local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

That won’t pick up on your changes.

Probably the quickest option would be to make that timer endless as well, then cancel both of them in gameover().