Search code examples
c#xna2d-games

XNA temporary pause


I need to do a temporary pause. If I do a collision with an enemy I want to pause the game for 1.5 seconds. Take a look here to my code:

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;
                }
            }
            //...
        }
}

It doesn't function! What could I modify?


Solution

  • Keep updating the timer, even when the game is paused. Furthermore, I would count backwards:

    if(timer <= 0) //!tPause
    {
        //...
        if(enemy.rectangle.Intersects(player.rectangle))
        {
            timer = 1500; //initialize a 1.5 second pause
        }
    }
    else
        timer -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;