Countdown Timer

Hi,

I found a countdown timer https://gist.github.com/coronarob/05accd04ce581b81572f

however I am running into issues when I enter the function into a scene and try to re-access the scene the clock.text stays the same and just picks off where It left off.

 I tried

 --composer.removeScene( "scene",true)

this works only in the simulator however when I build it the app crashes if I try to go back.

Is there a way to empty all my variables out or to just restart the screen as new? 

You’re running into an issue where the top level code in a scene module does not re-execute when the scene is revisited.  So this line:

local secondsLeft = 20 * 60 – 20 minutes * 60 seconds

Will only ever execute once unless you remove the scene. What you need to do is inside the scene:show() function reset the value. Assuming you leave that line near the top of the scene and do this:

function scene:show( event )     local sceneView = self.view     if event.phase == "will" then         secondLeft = 20 \* 60 -- or whatever value you want     end end

Then each time you re-enter the scene the value will reset.

The line:

--composer.removeScene( "scene",true)

if you blindly put that in, won’t do anything. The two minus-signs (–) say "Comment this  line out and ignore it

Next “scene” is going to try and remove a scene that is in the “scene.lua” file. It doesn’t make sense to have a scene named “scene”.

Rob

 local function updateTime() -- decrement the number of seconds clockText.isVisible=true clockText:setFillColor( math.random(1,250)/250, math.random(1,250)/250, math.random(1,250)/250 ) -- time is tracked in seconds. We need to convert it to minutes and seconds secondsLeft = secondsLeft - 1 print(secondsLeft) 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 sceneGroup:insert(clockText) --clockText.isVisible=false print("printing time display "..timeDisplay) if(timeDisplay=="00:00") then endtimesheet() end end -- run them timer local countDownTimer = timer.performWithDelay(1000, updateTime, secondsLeft)

I am getting the same issues, is there a way to just restart the screen completely 

I found a solution by including the timer 

if event.phase == "will" then secondsLeft = 100 -- or whatever value you want local countDownTimer = timer.performWithDelay(1000, updateTime, secondsLeft) end

what do you think?? 

You’re running into an issue where the top level code in a scene module does not re-execute when the scene is revisited.  So this line:

local secondsLeft = 20 * 60 – 20 minutes * 60 seconds

Will only ever execute once unless you remove the scene. What you need to do is inside the scene:show() function reset the value. Assuming you leave that line near the top of the scene and do this:

function scene:show( event )     local sceneView = self.view     if event.phase == "will" then         secondLeft = 20 \* 60 -- or whatever value you want     end end

Then each time you re-enter the scene the value will reset.

The line:

--composer.removeScene( "scene",true)

if you blindly put that in, won’t do anything. The two minus-signs (–) say "Comment this  line out and ignore it

Next “scene” is going to try and remove a scene that is in the “scene.lua” file. It doesn’t make sense to have a scene named “scene”.

Rob

 local function updateTime() -- decrement the number of seconds clockText.isVisible=true clockText:setFillColor( math.random(1,250)/250, math.random(1,250)/250, math.random(1,250)/250 ) -- time is tracked in seconds. We need to convert it to minutes and seconds secondsLeft = secondsLeft - 1 print(secondsLeft) 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 sceneGroup:insert(clockText) --clockText.isVisible=false print("printing time display "..timeDisplay) if(timeDisplay=="00:00") then endtimesheet() end end -- run them timer local countDownTimer = timer.performWithDelay(1000, updateTime, secondsLeft)

I am getting the same issues, is there a way to just restart the screen completely 

I found a solution by including the timer 

if event.phase == "will" then secondsLeft = 100 -- or whatever value you want local countDownTimer = timer.performWithDelay(1000, updateTime, secondsLeft) end

what do you think?? 

I have a question guys, this works perfectly with minutes and seconds, but I can’t figure out how to add hours it shows 100+ minutes

If you have seconds, to get hours do an integer divide by 3600 (60 * 60):

local hours = math.floor( seconds / 3600 )

local remainingSeconds = seconds - (hours * 3600)

local minutes = math.floor( remainingSeconds / 60)

remainingSeconds = remainingSecondsd % 60

or something like that.

I have a question guys, this works perfectly with minutes and seconds, but I can’t figure out how to add hours it shows 100+ minutes

If you have seconds, to get hours do an integer divide by 3600 (60 * 60):

local hours = math.floor( seconds / 3600 )

local remainingSeconds = seconds - (hours * 3600)

local minutes = math.floor( remainingSeconds / 60)

remainingSeconds = remainingSecondsd % 60

or something like that.