Hello all,
So i am just trying to get a handle on using time for a future version.
I looked at the sample apps “clock” and “Timer” and cobbled together the below code into a main.lua file
and got it working ie displays time since starting in countup or countdown options.
So i tried to put it in an app and yes it displayed - was also able to freeze the dispaly once the game had finished - however the timer was still working (terminal print every 1000).
So i finished and pushed the menu button which changes storyboard scenes - then the app crashes ( at a line in the new scene that has another timer.performwith delay
- attempt to call field ‘performWithDelay’ (a nil value) error.
Without the timer the app works so the problem is with the new code - and i am guessing because
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 ) straight from sample code
also tried
local clockTimer = timer.performWithDelay( 1000, updateTime, 0 )
does not have a stop point.
So how do you correctly stop a timer.performWithDelay within storyboard?
I tried making things nil - but My attempt at that did not work - maybe i do it wrong!
(admission - i use storyboard.purgeOnSceneChange = true for alot of cleanup)
also tried timer.cancel ( clockTimer ) within exit
[lua]
function scene:exitScene( event )
local group = self.view
-----------------------------------------------------------------------------
– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
-----------------------------------------------------------------------------
timer.cancel ( clockTimer )
end
[/lua]
but that did not work- attempt to call field ‘cancel’ (a nil value) error
Any obvious mistakes anyone?
thanks
T.
[lua]
main.lua
timerHr = display.newText(" 00:00", 0, 0, native.systemFont, 20 )
timerHr:setTextColor( 131, 255, 131, 255 )
timerHr.x = 220
timerHr.y = 100
second = 0
minute = 0
hour = 0
function updateTime()
– this part works as a countdown
–[[
second = second - 1
if second == -1 then second = 59; minute = minute - 1 end
if minute == -1 then second = 59; minute = 59; hour = hour - 1 end
if second < 10 then
secondD = 0 … second
else
secondD = second
end
if minute < 10 then
minuteD = 0 … minute
else
minuteD = minute
end
if hour < 10 then
hourD = 0 … hour
else
hourD = hour
end
–]]
– this part works as a countup
—[[
second = second + 1
if second == 60 then second = 0; minute = minute + 1 end
if minute == 60 then second = 0; minute = 0; hour = hour + 1 end
if second < 10 then
secondD = 0 … second
else
secondD = second
end
if minute < 10 then
minuteD = 0 … minute
else
minuteD = minute
end
if hour < 10 then
hourD = 0 … hour
else
hourD = hour
end
–]]
if hour > 0 then
timer = hourD … “:” … minuteD … “:” … secondD
else
timer = " " … minuteD … “:” … secondD
end
print( timer)
timerHr.text = timer
end
local clockTimer = timer.performWithDelay( 1000, updateTime, -1 )
[/lua]