Timer.performwithDelay / storyboard

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]

Nothing looks wrong at first glance. My only thought is that you have the local variable “clockTimer”, it being local could be the cause of the problem, depending on where it is created. If you created that timer in “createScene” for example, the exitScene function won’t recognise it.

But otherwise, 

timer.cancel(clockTimer) clockTimer = nil

in the exitScene is what I would do as well, so it should stop the timer.

If making it local is causing you problems, you could always put: 

local clockTimer

at the top of your lua file (outside of any functions), and then use:

clockTimer = timer.performWithDelay(1000, updateTime, -1 )

when you need to create it. That way it will not be a global variable, but can still be accessed in multiple functions within the lua file.

You by any chance don’t have a variable named “timer” in your storyboard scene?  You can have multiple timers.  The “- attempt to call field ‘performWithDelay’ (a nil value) error” indicates that the timer object/table has lost it’s reference to the performWithDelay method.  This frequently happens when you use a variable name that’s the same as an object or maybe you have a line like: 

timer = nil

somewhere in your code.

Rob had the answer - line 65 - i used the variable timer to concate my hours, mins, & secs ready for display. Changed the name and it works now - only difference is that the terminal output doesn’t actually show the time but "table: 0x… " 

But it works.

thanks all

T.

Nothing looks wrong at first glance. My only thought is that you have the local variable “clockTimer”, it being local could be the cause of the problem, depending on where it is created. If you created that timer in “createScene” for example, the exitScene function won’t recognise it.

But otherwise, 

timer.cancel(clockTimer) clockTimer = nil

in the exitScene is what I would do as well, so it should stop the timer.

If making it local is causing you problems, you could always put: 

local clockTimer

at the top of your lua file (outside of any functions), and then use:

clockTimer = timer.performWithDelay(1000, updateTime, -1 )

when you need to create it. That way it will not be a global variable, but can still be accessed in multiple functions within the lua file.

You by any chance don’t have a variable named “timer” in your storyboard scene?  You can have multiple timers.  The “- attempt to call field ‘performWithDelay’ (a nil value) error” indicates that the timer object/table has lost it’s reference to the performWithDelay method.  This frequently happens when you use a variable name that’s the same as an object or maybe you have a line like: 

timer = nil

somewhere in your code.

Rob had the answer - line 65 - i used the variable timer to concate my hours, mins, & secs ready for display. Changed the name and it works now - only difference is that the terminal output doesn’t actually show the time but "table: 0x… " 

But it works.

thanks all

T.