Niranjana
Niranjana

Reputation: 536

Timer running when game is in pause state-LibGdx

I created timer like this in my game:

    Timer.schedule(new Task() {
        @Override
        public void run() {
            makeComets();
        }
    }, 2);

I am using these code for more than 2 time in the entire game and it is working fine.

But whenever I pause the game,this timer is not getting stopped.Timer is running while in pause state.This affects the entire game play.

I found out Timer.instance().stop() and Timer.instance().stop() methods are there.

I made these call at ApplicationListener's pause and resume callbacks.

@Override
public void pause() {
    gameState = GameState.PAUSE;
timerdelay = TimeUtils.nanosToMillis(TimeUtils.nanoTime());
    Timer.instance().stop();
}

@Override
public void resume() {
    gameState = GameState.RESUME;
  Timer.instance().delay(TimeUtils.nanosToMillis(TimeUtils.nanoTime()) - 
  timerDelay);
    Timer.instance().start();
}

But my problem is not completely solved.Still timer running when game state PAUSE is made manually.It works with device pause and resume. How can I resolve this issue?

Upvotes: 0

Views: 219

Answers (1)

AAryan
AAryan

Reputation: 20140

Your above scheduled task run once with a delay of 2 seconds.

But when you run repetitive task, you don't need to pause and resume your task because your task is scheduled on TimerThread that is managed by LibGDX framework itself.

EDIT

You need to stop Timer by yourself because LibGDX started for you. Then start Timer after some time according to your requirement.

@Override
public void resume() {
    Timer.instance().stop();
    stage.addAction(Actions.sequence(Actions.delay(5), Actions.run(new Runnable() {
        @Override
        public void run() {
            Timer.instance().start();
        }
    })));
}

Upvotes: 0

Related Questions