7
\$\begingroup\$

I have the pause function implemented and it works correctly in that it dims the player screen and stops updating the gameplay.

The problem is that GameTime continues to increase while it is paused, so my method that checks gameTime versus previousSpawnTime before spawning another enemy gets messed up and if the game is paused too long it is noticeable that the next enemy draws far too early.

Here is my code for the enemy update.

private void UpdateEnemies(GameTime gameTime)
    {
        // Spawn a new enemy every 1.5 seconds
        if (gameTime.TotalGameTime - previousSpawnTime > enemySpawnTime)
        {
            previousSpawnTime = gameTime.TotalGameTime;

            // Add an Enemy
            AddEnemy();
        }
...

I also have other methods that depend on gameTime. I've tried getting the total pause time and subtracting that from the total game time, but I can't seem to get it to work correctly if that is the way I should go about solving this.

If you need to see any other code let me know. Thank you.

\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

Instead of using TotalGameTime use ElapsedGameTime and accumulate the running total yourself in some variable. Then when you pause the game all you have to do is to stop adding the elapsed time to the variable. Here's an example:

float spawnTimer = 0.0f;
float timeBetweenSpawns = 1.5f;

private void Update(GameTime gameTime)
{
    // Perform actions that should only happen when the game is not paused
    if(!isPaused)
    {
        // Add time elapsed since previous frame to running timer
        spawnTimer += (float) gameTime.ElapsedGameTime.TotalSeconds;

        // Check if enough time has passed to spawn a new enemy
        if(spawnTimer >= timeBetweenSpawns)
        {
            AddEnemy();

            // Decrement the timer to prepare it for the next spawn
            spawnTimer -= timeBetweenSpawns;
        }
    }

    // Perform any actions that should happen all the time
}
\$\endgroup\$
6
  • 3
    \$\begingroup\$ Calling UpdateEnemies with a 0.0 time delta is a nicer option, it lets enemies get processing for incidental or flavor activities like tapping their feet in boredom, without affecting game play. If you simply stop calling updates then you lose interesting options like that. \$\endgroup\$ Commented Mar 26, 2012 at 21:05
  • \$\begingroup\$ @PatrickHughes Yes, I like the idea of lots of guys mid-battle tapping their feet waiting for you to unpause. \$\endgroup\$
    – House
    Commented Mar 26, 2012 at 21:17
  • 1
    \$\begingroup\$ @PatrickHughes The way I chose to demonstrate pausing was merely illustrative, as the question was mostly concerned with managing time. Nonetheless I still would not pass a zero time delta for two reasons: GameTime is calculated and managed automatically by the framework, so passing it a zero delta would require instancing a separate GameTime object or changing the signature to use a float; and even with a zero delta it would still perform the addition and timer checks needlessly. If needed I'd simply wrap part of the method in a pause check, so that the other half could continue updating. \$\endgroup\$ Commented Mar 26, 2012 at 21:21
  • 2
    \$\begingroup\$ @PatrickHughes What you are describing is clearly a view concept. When game is paused, render models with "bored-idle" animations. This should be handled in your Draw calls, NOT your Update calls. \$\endgroup\$ Commented Mar 26, 2012 at 22:10
  • 1
    \$\begingroup\$ @PatrickHughes Yes it is a bit odd, which is why in all of my projects the first thing I do at the top of my update loop is to convert the GameTime object into a simple float and use that value instead. I've never had the need but it does make things like implementing slow motion easier - just scale the elapsed time value by some constant. As for pausing the game I guess it's more a matter of personal preference - I like treating the pause like an on/off switch rather than a zero delta, in order to explicitly skip time-dependent computations, but both are perfectly valid solutions :) \$\endgroup\$ Commented Mar 26, 2012 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .