2016-03-14 48 views
1

我正在嘗試使整數變小時我的當前計數器/定時器加速。例如,它會像 10 ......... 9 ........,8 ........,7 ....... 6 .... .. 5 ..... 4 .... 3 ... 2 .. 1.加快計時器?

當前代碼:

private int interval; 
private Timer timer; 

public void startTimer(final Player p, String seconds) { 
    String secs = seconds; 
    int delay = 1000; 
    int period = 1000; 
    timer = new Timer(); 
    interval = Integer.parseInt(secs); 
    timer.scheduleAtFixedRate(new TimerTask() { 

     @Override 
     public void run() { 
      p.sendMessage("" + setInterval(p)); 
     } 

    }, delay, period); 
} 

private final int setInterval(Player p) { 
    if (interval == 1){ 
     timer.cancel(); 
     p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Countdown Finished!"); 
    } 
    return --interval; 
} 
+3

你得到任何錯誤或你有問題嗎? – Rishi

+0

如果我錯了,請原諒我,但是您不是隻打印'n'個週期,其中'n'是定時器上剩餘的秒數? –

+0

@MichaelA請說明你的問題。你想在倒計時期間縮短每個間隔的時間嗎? –

回答

1

From the Java docs:

public void scheduleAtFixedRate(TimerTask task, 
        Date firstTime, 
        long period) 

安排指定的任務重複執行固定利率,從指定時間開始執行。隨後的執行發生在大約定期,由指定的時間段隔開。

使用此方法,您無法更改週期,因爲此方法不是爲此設計的。如果您嘗試訪問任務內的句點,編譯器將會失敗,因爲句點在任務中不可見。

如果你不想圍繞線程,runnables和wait()方法換個頭,並且想要堅持使用Timer類中的方法(我假設你使用了這個方法 - 但是對於記錄來說,請如果使用另一個可能有文檔的軟件包中的方法,請將導入添加到已發佈的源代碼中!),請考慮使用public void schedule(TimerTask task, long delay),而不是將其封裝在while循環中。然後,您可以更改循環內的延遲參數,並計劃在不同的時間跨度內執行任務。

當然,當倒計時完成時(一個「快速&髒」的方式將使用布爾值),你必須弄清楚如何離開while循環。但是因爲你在while循環的每一次迭代中基本上「重置」定時器,timer.cancel()將不會爲你做任何事情。它會取消一次,但下一個迭代計時器將簡單地重新啓動任務。

1

一個選項是使用javafx.animation.Timeline

例如:

import javafx.animation.Interpolator; 
import javafx.animation.KeyFrame; 
import javafx.animation.KeyValue; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class CountDown extends Application { 

    private static final double SPEED_UP_FACTOR = 0.15; // + 15% 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     IntegerProperty counter = new SimpleIntegerProperty(10); 
     Timeline timeline = new Timeline(
       new KeyFrame(Duration.seconds(counter.get()), new KeyValue(counter, 0)) 
     ); 
     counter.addListener((observable, oldValue, newValue) -> { 
      double currentRate = timeline.getRate(); 
      timeline.setRate(currentRate + currentRate * SPEED_UP_FACTOR); // speed up count down 
      System.out.println(newValue); 
     }); 
     timeline.setOnFinished(event -> { 
      System.out.println("CountDown Finished!"); 
      Platform.exit(); 
     }); 
     timeline.play(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 

編輯

或簡單地使用Thread.sleep()

public class CountDown { 

    private static final double SPEED_UP_FACTOR = 0.15; // + 15% 

    public static void main(String[] args) { 
     CountDownTimer timer = new CountDownTimer(10, 1000); 
     new Thread(timer).start(); 
    } 

    static final class CountDownTimer implements Runnable { 

     final int initialValue; 
     final long intervalMillis; 

     CountDownTimer(int initialValue, long intervalMillis) { 
      this.initialValue = initialValue; 
      this.intervalMillis = intervalMillis; 
     } 

     @Override 
     public void run() { 
      int counter = initialValue; 
      double rate = 1; 
      while (counter > 0) { 
       sleep(Math.round(intervalMillis/rate)); 
       rate += rate * SPEED_UP_FACTOR; 
       System.out.println(--counter); 
      } 
     } 

    } 

    private static void sleep(long timeout) { 
     try { 
      Thread.sleep(timeout); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

}