2014-11-21 95 views
0

我做這個代碼如何在LibGDX暫停定時器delaySeconds

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

當我的遊戲暫停我這樣做的抽獎方法(演員)

Timer.instance().stop(); 

在恢復比賽我這樣做:

Timer.instance().start(); 

它可以工作,但是當我在2秒鐘後暫停並在35秒後恢復時,函數flyAway()立即執行,不計算剩餘的10秒。

我怎麼可以暫停delaySeconds。謝謝。

對不起,我的英語不好。

FlyAvay代碼

//Муха улетает 
public void flyAway(){ 
    flyAway = 1; 

    targetFlyX = Gdx.graphics.getWidth()/2; 
    targetFlyY = Gdx.graphics.getHeight() + 300; 
    this.setColor(1f, 1f, 1f, 0.2f); 
    this.addAction(sequence(moveTo(targetFlyX, targetFlyY, flySpeed), desctroyFlyAction));  
} 

Action desctroyFlyAction = new Action(){ 
    public boolean act(float delta) { 
     removeFly(); 
     return true; 
    } 
}; 

public void removeFly(){ 
    rectBounds.set(1, 1, 0, 0); 
    this.remove(); 
} 
+0

與我們分享您的flyAway()代碼。 – 2014-11-21 14:01:57

+0

完成.......... – KajeNick 2014-11-21 15:58:43

+0

代碼對我來說看起來很好。嘗試打印任務的'getExecuteTimeMillis()'(請參閱https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/utils/Timer.java#L192) )'和'start()'(它應該在任務運行前顯示毫秒) – 2014-11-22 06:59:38

回答

3

我有這個問題,我知道這是晚,但也許它會幫助別人。問題是,當創建Timer時,會給系統時鐘一段時間執行,而不是指定的時間量,直到它執行。因此,當您在pause方法中調用Timer.stop時,請等待35秒,然後在resume方法中調用Timer.start,該任務應該完成的時間已經過去,因此它立即執行。

我的解決辦法只是保存當前系統時間pause,然後從當前時間減去它resume,並再次調用Timer.start()之前添加的差作爲延遲使用Timer.delay()Timer

public void pause() { 
    timerDelay = TimeUtils.nanosToMillis(TimeUtils.nanoTime()); 
    Timer.stop(); 
} 

public void resume() { 
    Timer.delay(TimeUtils.nanosToMillis(TimeUtils.nanoTime()) - timerDelay); 
    Timer.start(); 
} 
+1

我有這個確切的問題,你的建議(叫timer.delay)像一個魅力工作,謝謝@KnowNothing! – fegemo 2016-11-18 21:47:39

+0

沒問題!很高興爲你工作。 – KnowNothing 2016-12-03 22:46:58