# How do I temporarily pause an XNA game?

Collision with an enemy should pause the game for 1.5 seconds.

My failed attempt:

bool tPause;
float timer;

public ovveride Update(GameTime gameTime) {
if(!tPause) {
// ...
if(enemy.rectangle.Intersects(player.rectangle)) {
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
tPause = true;
if(timer > 1500) {
tPause=false;
timer=0;
}
}
// ...
}
}


What am I missing? How can I do this?

• This code snippet says that when it IS paused it can never NOT be paused. Time to mess with some breakpoints and test maybe? – Dialock Jun 8 '14 at 17:24

Once there is a collision, the timer variable will not get updated anymore, because the code segment of your if statement will not be executed.

Instead, you can use an else statement, where you exectue the code, to update your timer variable.

bool tPause;
float timer;

public ovveride Update(GameTime gameTime)
{
if(!tPause)
{
if(enemy.rectangle.Intersects(player.rectangle))
{
tPause = true;
timer = 0;
}
}
else // if you are in the paused state
{
// update the timer variable and check, if 1.5 seconds passed
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > 1500)
{
// set the paused state back to false
tPause = false;
}
}
}