Sunday, February 11, 2018

pausing - How do I pause an XNA game?


I'm trying to implement pausing by following this tutorial. The tutorial recommends essentially this as the implementation:


if (!paused) {
Simulate(gameTime);
}


How does this work? What does Simulate mean here?



Answer



It's just a simple way to wrap your entire Update method in a conditional, without mixing the pausing logic with your game's updating logic.


So instead of:


protected override void Update(GameTime gameTime)
{
if(!paused)
{
// Lots and lots of code!

}
}

You can have:


private void Simulate(GameTime gameTime)
{
// Lots and lots of code!
}

protected override void Update(GameTime gameTime)

{
if(!paused)
{
Simulate(gameTime);
}
}

Of course, if you would prefer to express it as "continue the game like normal", you could also do this:


protected override void Update(GameTime gameTime)
{

if(paused)
return;

// Lots and lots of code!
}

These all do the same thing. Which method you choose to do it is simply a matter of taste. Basically what the tutorial is telling you is:



  • Determine if your game should be paused and store that in a variable (ie: bool paused)

  • If it is paused, skip over the code that is responsible for updating your game's state (essentially freezing it in time, ie: "pausing" it.)



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...