Stop Countdown Timer

When a player uses a life, I have a timer that counts down the minutes and seconds until the life is replaced.

Since the player has 5 lives, when one life is replaced, if other lives have been used, the timer should display the time of the next life used (the one with the least amount of time).

However, the timer doesn’t stop at 0 and it goes negative and doesn’t switch.  But if I relaunch, the appropriate timer for the appropriate game life is properly displayed.

Any thoughts on how to fix this?

Thanks

Lori

We would need to see your code on how your canceling your timer and resetting it.

Rob

You say you are using a timer, so you must be either repeating the timer or looping some how.

When do your stop / pause your timer?

Can you post your code so we can see what the issue may be.

Larry

Here is the code I’m using for the timer.  I am not sure how to stop it.  I’ve tried a few different approaches but it freezes the timer or causes some other issue.  I appreciate the help!


When the player uses a game life, I store the time for the game Time used; there are five game times.  

Then  I load the game times so prevgameTime1 below is loaded before the timer starts.

local now = os.time()

local secondsUntilNext = (1800 -(now-prevgameTime1))                  

local leftOverSeconds = secondsUntilNext%60  --the % returns the remainder of the division

local numberOfMinutes = (secondsUntilNext -leftOverSeconds)/60

numberOfMinutes = string.format("%02i", numberOfMinutes)

leftOverSeconds = string.format("%02i", leftOverSeconds) 

GameTime1Text.text = (numberOfMinutes…":"…leftOverSeconds)

Thanks,

Lori

Okay, so you’re not using a timer.performWithDelay() to do this, just using the # of seconds that os.time() returns.  How are you looping to show the change in time?  Are you using an enterFrame event listener?  Where is prevgameTime1 being created?  I would think you need to adjust that value to reset this.

Rob

@lgalcott

Ok so as I understand it what you are trying to do is this

User has 5 lives.

Life 1, Life 2, Life 3, Life 4 , Life 5

So if a user looses a life and now has 4 lives a counter starts counting down from say ( 3 min = 180 seconds ).

now during this count down the users looses life 4 and now has 3 lives left.

When Life 5 is lost a counter should start a 180 second count down to replace this lost life.

When Life 4 is lost a counter should start a 180 second count down to replace this lost life.

So from as I understand it you could have 2 timers going at one time to replace the lost lives.

So timer 5 - counting down could be at 30 seconds to replace life 5.

Then life 4 is lost and now it starts a counter at 180 to replace life 4 - so we now only have 3 lives.

when timer for Life 5 reaches down to zero then you now have 4 lives again

when timer for Life 4 ( from above ) reaches zero you now have 5 lives again.

is this correct? is this what you are trying to do?

If so doing the calculations your self will be a massive headache, and very buggy.

There are a couple ways you can do this, but the easiest I can think of is to use the timer.performWithDelay() to update a lost life with something like this.

Some code some how executes RemoveUsersLife local function RemoveUsersLife() local function RestoreUsersLife( event ) &nbsp;&nbsp;&nbsp; print( "listener called" ) end &nbsp;&nbsp;&nbsp; int\_UsersLives = int\_UsersLives -1 -- remove one of the users lives if UsersLives \< 1 then CallGameOverLogic(); else &nbsp;&nbsp;&nbsp; ---Start Timer to restore users life &nbsp;&nbsp;&nbsp; timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) I think end end --- 

If you need to have a countdown shown on the screen at the same time, then I need to think about how to track multiple of those to display.

Good luck

Larry

[quote=“doubleslashdesign,post:6,topic:327690”]

@lgalcott

Ok so as I understand it what you are trying to do is this

User has 5 lives.

Life 1, Life 2, Life 3, Life 4 , Life 5

So if a user looses a life and now has 4 lives a counter starts counting down from say ( 3 min = 180 seconds ).
now during this count down the users looses life 4 and now has 3 lives left.

When Life 5 is lost a counter should start a 180 second count down to replace this lost life.
When Life 4 is lost a counter should start a 180 second count down to replace this lost life.

So from as I understand it you could have 2 timers going at one time to replace the lost lives.

So timer 5 - counting down could be at 30 seconds to replace life 5.
Then life 4 is lost and now it starts a counter at 180 to replace life 4 - so we now only have 3 lives.

when timer for Life 5 reaches down to zero then you now have 4 lives again
when timer for Life 4 ( from above ) reaches zero you now have 5 lives again.

is this correct? is this what you are trying to do?

If so doing the calculations your self will be a massive headache, and very buggy.

There are a couple ways you can do this, but the easiest I can think of is to use the timer.performWithDelay() to update a lost life with something like this.

Some code some how executes RemoveUsersLife local function RemoveUsersLife() local function RestoreUsersLife( event ) print( "listener called" ) end int\_UsersLives = int\_UsersLives -1 -- remove one of the users lives if UsersLives \< 1 then CallGameOverLogic(); else ---Start Timer to restore users life timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) I think end end ---

If you need to have a countdown shown on the screen at the same time, then I need to think about how to track multiple of those to display.

Good luck

Larry [/quote]
I think this is a nice approach, however don’t forget about the potential of the game being suspended for an unknown amount of time, in which case, these timers would not be running.

So your still going to have to factor in the time between when the app was suspended and the time it was resumed at.

Cheers

Yep, Gremlin Interactive is correct you would want to create a table of timer handles that way you can loop through and pause them or cancel them.

Just a brief example - not sure on the syntax ( at 7am with little sleep )

myTimers = {} myTimers(#myTimers) = timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) --during a pause for i=1,@myTimers do local result = timer.pause( myTimers(i) ) &nbsp;&nbsp; print( "Time remaining is " .. result ) end

Thank you for responding!   I can tell by reading that your answers took some time to generate so I really appreciate the input and the time it took to respond.

You are correct, I can have as many as five timers going on behind the scene but only one timer is displayed at any given moment.  When a timer reaches zero minutes and zero seconds, the goal is to have it switch to the next timer that is active with the least remaining time.

I’m storing the time for each of the game times used so if there is a pause in the game for some reason, when it resumes the timer on display has been taking the suspended time into consideration.  For example, when I shut down the game and restart it a various points in the future, the timer always starts where it is supposed to be.   It also makes the change from displaying the time for game life 1 to displaying the game life time for game life 2.When I suspend the game or shut it down it works and the timer shuts itself but I’m having trouble making it make the switch without shutting down.  I believe it is working during a suspended period because the game times are stored and reloaded in a runtime event.  However, after reading the responses above, I went back and looked at my code and I think I found where I might be able to fix my code.

I have a Runtime event that replaces the game lives when:  if now > (prevgameTime1 + 1800) then it increases the amount of available game lives.  This code works smoothly.  I also reset the game Time and save it during this event.  The code doesn’t address what happens if the time goes negative, so possibly I can add and else statement to this event.  I’ll give it a try when I get home.  Before I was trying to address the problem using the numberofMinuts and leftOverSeconds above.

Lori

in that case you could create a timer table with properties to help you manage them

timersList= {} timersList[1].alive = true; timersList[1].currentCount = 180 -- where to start counting down from when dead timersList[1].timerHandle = nil; timersList[2].alive = true; timersList[2].currentCount = 180 -- where to start counting down from when dead timersList[2].timerHandle = nil; etc... local function RemoveUsersLife() ---- not exact but something like this local function RestoreUsersLife( event ) print( "listener called" ) int\_UsersLives = int\_UsersLives + 1 timersList[int\_UsersLives].alive = true; end int\_UsersLives = int\_UsersLives -1 -- remove one of the users lives if UsersLives \< 1 then CallGameOverLogic(); else ---Start Timer to restore users life timersList[int\_UsersLives+1].timerHandle = timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) I think timersList[int\_UsersLives+1].alive = false; end end local updateUserLifeCounter= function( event ) --The enter frame event fires for every frame you can modify the counter text here -- checking each item in the table for x = 1, #timersList do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if timersList[x].active == false then timersList[x].currentCount = timersList[x].currentCount -1 end &nbsp;&nbsp;&nbsp;&nbsp;end end Runtime:addEventListener( "enterFrame", updateUserLifeCounter) I think you get the idea based on the code above, at least its a start. Hope this helps and instead of confuses you :)

Larry

WOW!  Thanks - I will give that a try tonight.   :slight_smile:

Issue Resolved.  Thank you for all the input!

We would need to see your code on how your canceling your timer and resetting it.

Rob

You say you are using a timer, so you must be either repeating the timer or looping some how.

When do your stop / pause your timer?

Can you post your code so we can see what the issue may be.

Larry

Here is the code I’m using for the timer.  I am not sure how to stop it.  I’ve tried a few different approaches but it freezes the timer or causes some other issue.  I appreciate the help!


When the player uses a game life, I store the time for the game Time used; there are five game times.  

Then  I load the game times so prevgameTime1 below is loaded before the timer starts.

local now = os.time()

local secondsUntilNext = (1800 -(now-prevgameTime1))                  

local leftOverSeconds = secondsUntilNext%60  --the % returns the remainder of the division

local numberOfMinutes = (secondsUntilNext -leftOverSeconds)/60

numberOfMinutes = string.format("%02i", numberOfMinutes)

leftOverSeconds = string.format("%02i", leftOverSeconds) 

GameTime1Text.text = (numberOfMinutes…":"…leftOverSeconds)

Thanks,

Lori

Okay, so you’re not using a timer.performWithDelay() to do this, just using the # of seconds that os.time() returns.  How are you looping to show the change in time?  Are you using an enterFrame event listener?  Where is prevgameTime1 being created?  I would think you need to adjust that value to reset this.

Rob

@lgalcott

Ok so as I understand it what you are trying to do is this

User has 5 lives.

Life 1, Life 2, Life 3, Life 4 , Life 5

So if a user looses a life and now has 4 lives a counter starts counting down from say ( 3 min = 180 seconds ).

now during this count down the users looses life 4 and now has 3 lives left.

When Life 5 is lost a counter should start a 180 second count down to replace this lost life.

When Life 4 is lost a counter should start a 180 second count down to replace this lost life.

So from as I understand it you could have 2 timers going at one time to replace the lost lives.

So timer 5 - counting down could be at 30 seconds to replace life 5.

Then life 4 is lost and now it starts a counter at 180 to replace life 4 - so we now only have 3 lives.

when timer for Life 5 reaches down to zero then you now have 4 lives again

when timer for Life 4 ( from above ) reaches zero you now have 5 lives again.

is this correct? is this what you are trying to do?

If so doing the calculations your self will be a massive headache, and very buggy.

There are a couple ways you can do this, but the easiest I can think of is to use the timer.performWithDelay() to update a lost life with something like this.

Some code some how executes RemoveUsersLife local function RemoveUsersLife() local function RestoreUsersLife( event ) &nbsp;&nbsp;&nbsp; print( "listener called" ) end &nbsp;&nbsp;&nbsp; int\_UsersLives = int\_UsersLives -1 -- remove one of the users lives if UsersLives \< 1 then CallGameOverLogic(); else &nbsp;&nbsp;&nbsp; ---Start Timer to restore users life &nbsp;&nbsp;&nbsp; timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) I think end end --- 

If you need to have a countdown shown on the screen at the same time, then I need to think about how to track multiple of those to display.

Good luck

Larry

[quote=“doubleslashdesign,post:17,topic:327690”]

@lgalcott

Ok so as I understand it what you are trying to do is this

User has 5 lives.

Life 1, Life 2, Life 3, Life 4 , Life 5

So if a user looses a life and now has 4 lives a counter starts counting down from say ( 3 min = 180 seconds ).
now during this count down the users looses life 4 and now has 3 lives left.

When Life 5 is lost a counter should start a 180 second count down to replace this lost life.
When Life 4 is lost a counter should start a 180 second count down to replace this lost life.

So from as I understand it you could have 2 timers going at one time to replace the lost lives.

So timer 5 - counting down could be at 30 seconds to replace life 5.
Then life 4 is lost and now it starts a counter at 180 to replace life 4 - so we now only have 3 lives.

when timer for Life 5 reaches down to zero then you now have 4 lives again
when timer for Life 4 ( from above ) reaches zero you now have 5 lives again.

is this correct? is this what you are trying to do?

If so doing the calculations your self will be a massive headache, and very buggy.

There are a couple ways you can do this, but the easiest I can think of is to use the timer.performWithDelay() to update a lost life with something like this.

Some code some how executes RemoveUsersLife local function RemoveUsersLife() local function RestoreUsersLife( event ) print( "listener called" ) end int\_UsersLives = int\_UsersLives -1 -- remove one of the users lives if UsersLives \< 1 then CallGameOverLogic(); else ---Start Timer to restore users life timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) I think end end ---

If you need to have a countdown shown on the screen at the same time, then I need to think about how to track multiple of those to display.

Good luck

Larry [/quote]
I think this is a nice approach, however don’t forget about the potential of the game being suspended for an unknown amount of time, in which case, these timers would not be running.

So your still going to have to factor in the time between when the app was suspended and the time it was resumed at.

Cheers

Yep, Gremlin Interactive is correct you would want to create a table of timer handles that way you can loop through and pause them or cancel them.

Just a brief example - not sure on the syntax ( at 7am with little sleep )

myTimers = {} myTimers(#myTimers) = timer.performWithDelay( 180000, RestoreUsersLife) -- 1000 milliseconds = 1 second (180,000 = 3 min) --during a pause for i=1,@myTimers do local result = timer.pause( myTimers(i) ) &nbsp;&nbsp; print( "Time remaining is " .. result ) end

Thank you for responding!   I can tell by reading that your answers took some time to generate so I really appreciate the input and the time it took to respond.

You are correct, I can have as many as five timers going on behind the scene but only one timer is displayed at any given moment.  When a timer reaches zero minutes and zero seconds, the goal is to have it switch to the next timer that is active with the least remaining time.

I’m storing the time for each of the game times used so if there is a pause in the game for some reason, when it resumes the timer on display has been taking the suspended time into consideration.  For example, when I shut down the game and restart it a various points in the future, the timer always starts where it is supposed to be.   It also makes the change from displaying the time for game life 1 to displaying the game life time for game life 2.When I suspend the game or shut it down it works and the timer shuts itself but I’m having trouble making it make the switch without shutting down.  I believe it is working during a suspended period because the game times are stored and reloaded in a runtime event.  However, after reading the responses above, I went back and looked at my code and I think I found where I might be able to fix my code.

I have a Runtime event that replaces the game lives when:  if now > (prevgameTime1 + 1800) then it increases the amount of available game lives.  This code works smoothly.  I also reset the game Time and save it during this event.  The code doesn’t address what happens if the time goes negative, so possibly I can add and else statement to this event.  I’ll give it a try when I get home.  Before I was trying to address the problem using the numberofMinuts and leftOverSeconds above.

Lori