PDA

View Full Version : Pause/Resume game


aeris
02-15-2005, 05:19 PM
Hey there

I was wondering if there is an easy way to pause a flash game until the user presses a button to resume. Actually, if this could be done throughout the game, by using one pause/resume button it would be great.

I have been reading about this for a while. I know that you can use setInterval to delay the movies. And I think you would need to define a function to do this for you. However, you also would need to stop()/play() every single object/movieclip in the game, so you can pause the entire game.

I really cant do that cause, firstly I have so many different movieclips in this game. Secondly most of them are constantly created dynamically depending on the data. So there is no way I can define them one by one.

Any help/ideas/tutorials will be so much appreciated.

Thanks!

ubergrafik
02-16-2005, 12:32 AM
Hmm, not sure. Maybe you could use javascript to pause the player? Maybe set the FPS to 0? Dunno, just ideas.

sasuke
01-18-2008, 02:17 PM
I create a component called PauseComponent. it can pause movie clip (with or without instaname!) onEnterFrame, mouse & key listener and sound.

self
01-18-2008, 02:24 PM
hmm you answered an entry which is nearly 3 yrs old... i guess he either fixed the problem or didnt use a pause function.

but since the component might be useful to others, why dont you attach it or post a link to it? ;)

sasuke
01-20-2008, 03:09 PM
i still working on it. counter same minor problem. i will post a link to it when i done.

vonWolfehaus
04-17-2008, 12:09 AM
Sorry for necrothreading, but I just ran into this problem myself and though I'd share my findings since there doesn't appear to be any solutions here yet. (I googled for solutions and this thread was the most legit result).

To pause an onEnterFrame- or Timer-based game (no the old mc-based types), remove the tick method to pause (and all others if they were added outside the tick method), and then add them back on resume. Here's the pause/resume portion from my own AS3 game:


public function tick(evt:Event):void
{
if (isFirst)
{
isFirst = false;
// initialize everything and add listeners
}
mainLoop.update(); // the method that update()'s all game pieces

if (!mouseState.onStage) // if the mouse leaves the stage, "pause"
{
removeEventListener(Event.ENTER_FRAME, tick);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved, false, 0, true);
}
}

public function mouseMoved(evt:MouseEvent):void // if the mouse returns to the stage, "resume"
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
addEventListener(Event.ENTER_FRAME, tick, false, 0, true);
}


This is simple enough. However, if you use getTimer() to achieve consistent game speeds (if physics-based eg velocity, friction, gravity etc) across framerates so that, for example, the player's ship will move just as fast at 10fps as 60fps, you'll need to watch out for your delta time (dt) variable.

Because the dt is based on getTimer(), it will keep increasing even if you stop the tick method. To solve this, I simply created some logic that fixes dt.

Modify the methods from before like so:


//-- CHANGE --add these vars
public var otime:int;
public var getThis:int;
public var getThat:int;

public function tick(evt:Event):void
{
[snip]
if (!mouseState.onStage) // if the mouse leaves the stage, "pause"
{
getThis = getTimer(); //-- CHANGE --get the time when the game was paused
removeEventListener(Event.ENTER_FRAME, tick);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved, false, 0, true);
}
}

public function mouseMoved(evt:MouseEvent):void // if the mouse returns to the stage, "resume"
{
getThat = getTimer(); //-- CHANGE --now get the time when it's resumed
otime = getThat - getThis; //-- CHANGE --grab the difference
mainLoop.fixTime(otime); //-- CHANGE --pass it to your main loop class that updates all other classes like Player(), Turret() etc

stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
addEventListener(Event.ENTER_FRAME, tick, false, 0, true);
}


And in the loop class just add this method:


public function fixTime(fixer:int):void
{
//-- in the update() method of this class, dt = getTimer() - time; so fix dt by modifying "time"
time += fixer; //-- now on the next update() dt will be caught up
}


I'm not sure if I explained this very clearly, so if you have any questions please let me know.

While this is pretty simple stuff, it caused me a little headache finding the bug, so I hope it helps others who googled the problem :)