2016-03-24 8 views
0

を得る:JavaFXのタイムラインは、私はJavaFXのを使用しており、現在、この時にこだわって、現在のサイクルタイム

Timeline timeline = new Timeline(); 
    timeline = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent actionEvent) { 
      System.out.println("Counting"); 
      //myFunction(currentCycleStep) <------ 
     } 
    }), new KeyFrame(Duration.seconds(SimulationController.speed))); 


    timeline.setCycleCount(5); 
    timeline.play(); 

我々はforループでタイムラインを使用することはできないので、私たちは().setCycleCountを使用する必要があります。だから私はカウントの現在のステップを得ることができますか?

答えて

0

実際のサイクルをタイムラインから取得する直接的な方法はないと思います。

あなた自身のプロパティを書くことができます。 以下は、currentTimeを監視し、 "direction"を変更するたびに自己生成プロパティの値をインクリメントする例です。

例:

オートリバース= falseを

currentTime: 1s 
currentTime: 2s 
currentTime: 3s 
currentTime: 0s <--------- increment actual cycle 
currentTime: 1s 

オートリバース=真

currentTime: 1s 
currentTime: 2s 
currentTime: 3s 
currentTime: 2s <--------- increment actual cycle 
currentTime: 1s 

私はすでに、これは少しトリッキーであることを、認める、多分この解決しなければなりませんあなたのニーズに合っています。

コード-例:

(あなたはそれがちょうどあなたが0から1にSimpleIntegerProperty-コンストラクタに渡すのparamを変更、1で開始する場合actualCycleは、0から始まる)

@Override 
public void start(Stage primaryStage) throws Exception { 
    SimpleStringProperty testProperty = new SimpleStringProperty(); 
    Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000), new KeyValue(testProperty, "1234"))); 
    timeline.setCycleCount(Timeline.INDEFINITE); 

    // ---------------------------------------------------------------------- 
    // we create our own property for the actual cycle-index 
    // ---------------------------------------------------------------------- 
    SimpleIntegerProperty actualCycleProperty = new SimpleIntegerProperty(0); 
    timeline.currentTimeProperty().addListener((observable, oldValue, newValue) -> { 
     boolean smaller = newValue.toMillis() < oldValue.toMillis(); 
     boolean evenCycleCount = actualCycleProperty.get() % 2 == 0; 
     if ((timeline.isAutoReverse() && !evenCycleCount && !smaller) 
       || ((evenCycleCount || !timeline.isAutoReverse()) && smaller)) { 
      actualCycleProperty.set(actualCycleProperty.get() + 1); 
     } 
    }); 

    actualCycleProperty.addListener((observable, oldValue, newValue) -> { 
     System.out.println(newValue); 
    }); 
    timeline.play(); 
} 
関連する問題