Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pausing a game

Started by cptrnet Dec 8, 2005 at 11:06 AM 5 replies 700+ views
Original Post
cptrnet
cptrnet
I haven't been programming lately and Im kind of rusty. I need to program a pause function for my game. I am using &#106avascript and html, just trying to move around a stick figure. Here is my game loop code.

function gameLoop(){

	clearTimeout(gameTimer);

	if(pauseKey.pressed){
		if(pause){
			pause = false;
		}else{
			pause = true;
		}
	}
	
	if(pause){
		gameTimer = setTimeout("gameLoop()",10);
	}else if(!gameOver){
		checkKeys();
		gravity();
		checkExit();
		checkEnemies();
		moveEnemies();
		gameTimer = setTimeout("gameLoop()",10);
	}
	
}


Whats happening is that its not pausing, you push the button it pauses for a second and then starts again. I know it's becuase the loop it going and its doing just what I've coded. I just can't think of anything to get around it.
If you insist on saying "DUH", try to make a post that makes a little more sense
frob
frob
If your game movement engine is separate from the display (which is the best design and I'm sure you implemented... [grin]) then you either stop calling the update function, or your function doesn't advance the timer if the simulation is paused.

Also, you would generally want to add a condition for too large of time steps, which is really handy for debugging.

#define MaxAllowedMotionStep (500)MotionEngine::Advance( int milliseconds ){  if( paused || milliseconds > MaxAllowedMotionStep )  {    // Don't advance the sim.    return;  }...


Of course, you'd need to do the &#106avascript version.<br><br>frob.
cptrnet
cptrnet
Just key down.
If you insist on saying "DUH", try to make a post that makes a little more sense
RabblePants
RabblePants
My best guess is that your pauseKey.pressed is probably true while the key is down. So your game loop might be processing the pauseKey twice: Once setting it and once unsetting it.

Instead what you should do is check that the pausekey was NOT pressed in the previous frame, but was pressed in the current (in other words, you're detecting a state change from not-pressed to pressed).

That way the body of the if statement is only executed once, and not multiple times :)

That's my guess at least :)

hth
Graf
cptrnet
cptrnet
Yah ill have to come up with some way of saying if its pressed and not paused then pause it, but then set a variable that wont unpause it until the key wasn't pressed anymore or something.
If you insist on saying "DUH", try to make a post that makes a little more sense
Rasmadrak
Rasmadrak
Since my game is using a delta-timebased approach, I simply make the delta = 0 when the game is paused...
this allows for cool stuff like walking around in an explosion, and rotating around etc. It has to be seen in motion, as it looks great!! :)
i.e the "Give order" menu in Knights of the old Republic is an example of what my system looks like when paused.

Slow motion is a breeze to do too... :D

Con's: Still using the same amount of resources even when paused, but this "system" should be considered as a feature in-game and not as a real "pause" function.



"Game Maker For Life, probably never professional thou." =)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.