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