Reputation: 413
In LibGdx, is there a way to pause the delta time when user pause the screen/left apps temporary (such an incoming call)? Take for example, when displaying message is going to take 10 secs for user to read the message, normally, I would get the message displayed start time, do the calculation in render() to get the elapsed time (currentTime - MessageStartedTime), if elapsed time > 10secs, then close message, everything works correctly, right. Imaging this scenario, user is reading the message (let assume elapse time is 2 secs), an incoming call that took 20 secs, when the process is brought back to my apps, the elapsed time > 10 secs, and therefore the message will be removed even though the message is only displayed for 2 secs.
so, my question is, is there a master time counter that I can implement in my application for this kind of purposes? and what is the best way to implement it?
Upvotes: 3
Views: 1554
Reputation: 1903
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.
Upvotes: 5
Reputation: 3183
private long pauseTime = 0;
private long pauseDelay = 0;
@Override
public void pause() {
pauseTime = System.nanoTime();
}
@Override
public void resume() {
pauseDelay = System.nanoTime() - pauseTime;
pauseTime = 0;
}
And in your render
method you just do long displayTime = delta - pauseDelay; pauseDelay = 0;
Upvotes: 1