Pausing a Game with Timers

6 posts

Flag Post

So right now the way this game works, basically, is that the Main class places the Level class on the stage once the play button has been clicked.

The Leven class has a few arguments including the amount of objects being created and the delay between each creation. This is done by creating a timer.

The problem is that also in the main class I have added a pause button that sets the framerate to 0 and stops the timer, as well as getting rid of events and etc. The problem is that when the game is unpaused, the timer is reset to 0.

Is there a good way to get around that, so the timer remembers its location? Why did Adobe not include that function?

Flag Post

With “timer reset to 0” what do you mean? The currentCount property is not affected if when using the stop() method.

Flag Post

Sorry I wasn’t clear.

What I mean to say is that the delay is restarted when the start() method is called.

So, the game stops, the frameRate is set to 0, the timer is stopped.

Then the game continues, frameRate is brought back to 24 and the timer starts(), however not from where it left off. So, if the player presses the pause button every 4 seconds (it’s a 5 second timer), the timer event listener function is never called.

So I guess the question is, how do you get a timer to remember where it was when it stopped and start it at that point?

Flag Post

Interesting problem. Unfortunately I can’t think of a way of solving it in a simple way using AS3’s Timer. I’d recommend building your own timer class which would have a start() and stop() method that behaves the way you want. Use flash.utils.getTimer to keep track of time and listen for ENTER_FRAME events. Or hope that someone else knows a better solution w/ AS3’s Timer.

Flag Post

It might be easier to handle all your timer needs through an enter-frame event. Then, when you kill the framerate to pause your game, everything will properly pick up where it’s supposed to when the game resumes.

Flag Post

I never use anything except time between frames (i.e. difference in lastTimer between enter-frame event calls) for in-game timing.