Problem refresh the timer every time the game is loaded

Guys, i have a problem on my timer and i cant figure out why my timer cant be refresh every time the game is started. It is working fine but will not refresh every time the game is loaded.

Here is the code fragment for the timer is in the file shown. This timer is placed into enterScene(event).

My tmr variable is declared as “local variable” outside the function. I even tried to cancel and remove the timer in exitScene but it just wont remove/refresh the timer. I have no idea why.

Can you guys help me out?  :frowning:

Hi @eryc1216,

In a previous reply, I suggested that you convert your app to Composer. Once you do, read particularly the section of the guide that talks about scene “life cycle”, how/when scene events are called, and how they are controlled within memory.

Best regards,

Brent

Hi @brent sorrentino, thanks for your reply! Im not so sure about Composer but i will have it checked later. My game is almost done but having problem the timer. Im sure that there must be something messed up but i cant figure out why… Can you help me with it?

[lua]

local tmr

function scene:enterScene(event)

   tmr = timer.performWithDelay(1000,

      function(e)

         output:setText(e.count) --setText is a function to show the counter on the screen

         if(e.count == 10) then

            stateText:setText(“Finished”)

            if tmr then

               timer.cancel(tmr)

               tmr = nil

            end

            storyboard.gotoScene(“restart”, “fade”, 400)

         end

      end, 10)

   function scene:exitScene(event)

      timer.cancel(tmr)

   end

end

[/lua]

Hi @eryc1216,

I surrounded your code with “lua” tags for clarity (a tip for your future postings):

[lua] --code [/lua]

After doing so, things don’t look quite correct. Is your exitScene() function really nested inside your enterScene() function? If so, this would explain why it’s never executing. Can you carefully check your code on this point?

Thanks,

Brent

Hi @Brent Sorrentino, thanks for the reply again. Sorry, its a typing error. The “end” on line 25 is supposed to be on line 18.

Hi @eryc1216,

Can you put some simple print() statements in each scene phase function to make sure they’re even being called, and when?

Thanks,

Brent

Hi @eryc1216,

In a previous reply, I suggested that you convert your app to Composer. Once you do, read particularly the section of the guide that talks about scene “life cycle”, how/when scene events are called, and how they are controlled within memory.

Best regards,

Brent

Hi @brent sorrentino, thanks for your reply! Im not so sure about Composer but i will have it checked later. My game is almost done but having problem the timer. Im sure that there must be something messed up but i cant figure out why… Can you help me with it?

[lua]

local tmr

function scene:enterScene(event)

   tmr = timer.performWithDelay(1000,

      function(e)

         output:setText(e.count) --setText is a function to show the counter on the screen

         if(e.count == 10) then

            stateText:setText(“Finished”)

            if tmr then

               timer.cancel(tmr)

               tmr = nil

            end

            storyboard.gotoScene(“restart”, “fade”, 400)

         end

      end, 10)

   function scene:exitScene(event)

      timer.cancel(tmr)

   end

end

[/lua]

Hi @eryc1216,

I surrounded your code with “lua” tags for clarity (a tip for your future postings):

[lua] --code [/lua]

After doing so, things don’t look quite correct. Is your exitScene() function really nested inside your enterScene() function? If so, this would explain why it’s never executing. Can you carefully check your code on this point?

Thanks,

Brent

Hi @Brent Sorrentino, thanks for the reply again. Sorry, its a typing error. The “end” on line 25 is supposed to be on line 18.

Hi @eryc1216,

Can you put some simple print() statements in each scene phase function to make sure they’re even being called, and when?

Thanks,

Brent