TIMER problems! Runs faster each time!

I am usuing the director class, and every time I leave a scene and come back through the menu, the timer runs faster. What is going on?


local function addUp()
gameTime = gameTime + 1;
timerText.text = gameTime
end
timeUp = timer.performWithDelay(1000, addUp, -1) [import]uid: 59735 topic_id: 16881 reply_id: 316881[/import]

Do you cancel the timer when you leave the scene and come back again?

Because you start it again when you shift to that scene, right? So it is running TWICE if you didn’t stop the first timer. [import]uid: 52491 topic_id: 16881 reply_id: 63247[/import]

How to I stop the timer? [import]uid: 59735 topic_id: 16881 reply_id: 63261[/import]

First, store the timer in a variable:

local timeUp = timer.performWithDelay(1000, addUp, -1)[/code]Then, use timer.cancel( timerId ), but make sure that your timerId variable is within scope:timer.cancel( timeUp )[/code] [import]uid: 52430 topic_id: 16881 reply_id: 63278[/import]

Sorry. Im still new at all this. I do not need to cancel timers that only run once or so right? Because they will already stop? Or is it good syntax to cancel them anyway? [import]uid: 59735 topic_id: 16881 reply_id: 63289[/import]

Even if I know a timer has ended, just as a good practice, I always do this somewhere:

if myTimer then timer.cancel( myTimer ) myTimer = nilend[/code] [import]uid: 52430 topic_id: 16881 reply_id: 63294[/import]

Thanks alot, and one more question if you do not mind. In the simulator, when I change the device from iPhone to anything else (even iphone 4) everything is the wrong size. /is there an easy fix? Last app I created I do not remember this problem. So like the background is only taking up 1/4 the screen when I change the device? [import]uid: 59735 topic_id: 16881 reply_id: 63312[/import]

What does your config.lua file look like?

Try this;

[lua]application =
{
content =
{
width = 320,
height = 480,
scale = “zoomEven”
}
}[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 16881 reply_id: 63713[/import]