1
\$\begingroup\$

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?

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

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;
        }
    }
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.