Countdown Help

What I’m trying to accomplish is that when you press the button, the time decreases to zero unless the button is pressed again and it resets to 200 and it tries to go back to zero (cycle). Right now, it is stacking so if I press it once and then again after 50 it will go down by 350, and it speeds up as I keep hitting the button. My goal is to get it so if you press it once, if keeps going down until zero and if it is tapped again before zero, it will go back to two hundred and try to go to zero, not a negative number.

Thanks for your help in advance.

[lua]function circle:touch(e)
if(time_left > 0)then
time_left = 200
gametmr = timer.performWithDelay(100, countDown, 200)
end
end

circle:addEventListener(“touch”, circle)[lua] [import]uid: 86518 topic_id: 31762 reply_id: 331762[/import]

To avoid it speeding up you will need to cancel the timer before calling it again. What you are currently doing is adding timer after timer on top of each other. Something like this should work

[lua]function circle:touch(e)
if(time_left > 0)then
time_left = 200
–check if gametmr already exists, if so cancel it
if gametmr ~= nil then
timer.cancel(gametmr)
gametmr = nil
end
gametmr = timer.performWithDelay(100, countDown, 200)
end
end

circle:addEventListener(“touch”, circle)[/lua] [import]uid: 147305 topic_id: 31762 reply_id: 126788[/import]

It works perfectly, I really appreciate it :slight_smile: [import]uid: 86518 topic_id: 31762 reply_id: 126789[/import]

@Hapiapps

Looks like budershank beat me to an answer, but just in case you are still listening I’d suggest a slight addition to the answer.

To be even more precise, be sure to keep track of the touch phase. Also, I suggest returning ‘true’ to indicate the touch has been handled and so it doesn’t fall through to another handler, unless that is what you want.

function circle:touch(e)  
 if(e.phase == "began") then  
 if gametmr then  
 timer.cancel(gametmr)  
 gametmr = nil  
 end  
  
 gametmr = timer.performWithDelay(100, countDown, 200)  
  
 end  
  
 return true  
  
end  

I put together a little demo of a solution to your question in SSKCorona (https://github.com/roaminggamer/SSKCorona)

To see it: http://www.youtube.com/watch?v=s2AsVEhy1Wc

The pertinent example code looks a little different, but is here:

 function sky:touch (event)   
 if( event.phase == "began" )then  
  
 if( theCountdownTimer.counting ) then  
  
 theCountdownTimer:stop()  
 theCountdownTimer:set( 42 ) -- Set timer (back) to 42 seconds  
  
 theCountdownTimer.counting = false  
 else  
 theCountdownTimer:autoCountDown( 0 )  
 theCountdownTimer.counting = true  
 end  
  
 end  
  
 return true  
 end  

Cheers,

edo out! [import]uid: 110228 topic_id: 31762 reply_id: 126810[/import]

Wow thats a really thorough answer. You must have put a lot of effort into that. Thanks! [import]uid: 86518 topic_id: 31762 reply_id: 126822[/import]

To avoid it speeding up you will need to cancel the timer before calling it again. What you are currently doing is adding timer after timer on top of each other. Something like this should work

[lua]function circle:touch(e)
if(time_left > 0)then
time_left = 200
–check if gametmr already exists, if so cancel it
if gametmr ~= nil then
timer.cancel(gametmr)
gametmr = nil
end
gametmr = timer.performWithDelay(100, countDown, 200)
end
end

circle:addEventListener(“touch”, circle)[/lua] [import]uid: 147305 topic_id: 31762 reply_id: 126788[/import]

It works perfectly, I really appreciate it :slight_smile: [import]uid: 86518 topic_id: 31762 reply_id: 126789[/import]

@Hapiapps

Looks like budershank beat me to an answer, but just in case you are still listening I’d suggest a slight addition to the answer.

To be even more precise, be sure to keep track of the touch phase. Also, I suggest returning ‘true’ to indicate the touch has been handled and so it doesn’t fall through to another handler, unless that is what you want.

function circle:touch(e)  
 if(e.phase == "began") then  
 if gametmr then  
 timer.cancel(gametmr)  
 gametmr = nil  
 end  
  
 gametmr = timer.performWithDelay(100, countDown, 200)  
  
 end  
  
 return true  
  
end  

I put together a little demo of a solution to your question in SSKCorona (https://github.com/roaminggamer/SSKCorona)

To see it: http://www.youtube.com/watch?v=s2AsVEhy1Wc

The pertinent example code looks a little different, but is here:

 function sky:touch (event)   
 if( event.phase == "began" )then  
  
 if( theCountdownTimer.counting ) then  
  
 theCountdownTimer:stop()  
 theCountdownTimer:set( 42 ) -- Set timer (back) to 42 seconds  
  
 theCountdownTimer.counting = false  
 else  
 theCountdownTimer:autoCountDown( 0 )  
 theCountdownTimer.counting = true  
 end  
  
 end  
  
 return true  
 end  

Cheers,

edo out! [import]uid: 110228 topic_id: 31762 reply_id: 126810[/import]

Wow thats a really thorough answer. You must have put a lot of effort into that. Thanks! [import]uid: 86518 topic_id: 31762 reply_id: 126822[/import]