I have two game states, they are:
GAME_RUNNING
GAME_PAUSED
When pause()
is called I set the state to GAME_PAUSED
and when resume()
is called I set the state to GAME_RUNNING
.
And then when I run update()
on my game I check this state and if it's paused I set delta to 0.
float deltaTime = game.state == GAME_PAUSED ? 0 : Gdx.graphics.getDeltaTime();
This means when all my game objects update, their changes are multipled by 0 and nothing changes. We can also add a condition and only update when deltaTime > 0
.
If you have a timer, you'll be updating this timer somehow like:
public void update(World world, float delta) {
frameTimer += delta;
if( frameTimer >= rate / 1000) {
fireAtEnemy(world);
frameTimer = 0;
}
}
Because delta is now 0 the timer will not increase and not increase until the game state it back to GAME_RUNNING
after resume()
.
Additionally, you can add an extra state called something like GAME_RUNNING_FAST
and when this state is set you can multiply the deltaTime
by 2 for example. If you have all your updates relative to the deltaTime
then your entire game will run at double speed. If that's something you'd desire.