转换结束时的 JavaFX 重复循环

Spe*_*lar 4 java transition loops javafx wait

我正在尝试执行以下循环:

  1. 更新一些变量
  2. 播放基于/表示变量的转换
  3. 转换完成后,转到 1

这将永远重复。我使它与以下工作:

//Step 3
transition.setOnFinished(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        doTheThing(); //Step 1, updates data
        transition.setToValue(data);
        transition.play(); // Step 2
    }
});

transition.play(); //Triggers first repeat
Run Code Online (Sandbox Code Playgroud)

当然,这是一个无限的递归循环,这不是一个好主意。问题是,一旦转换完成,我不知道如何触发重复。我试过使用循环:

while (1==1){
    doTheThing();
    transition.setToValue(data);
    transition.play();
}
Run Code Online (Sandbox Code Playgroud)

但这不仅不等待转换(在意料之中,对我来说不是问题),它根本不播放转换,并且程序没有响应。我也试过这个:

transition.setOnFinished(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        ready = true;
    }
});
while (1==1){
    if (ready){
        ready = false;
        doTheThing();
        transition.setToValue();
        transition.play();
    }
}
Run Code Online (Sandbox Code Playgroud)

但它的作用与解决方案#2 相同。我宁愿不编程等待,但即使我这样做了,我也不确定如何在循环重复之前等待,同时不停止播放过渡。

我可以做什么?

eck*_*kig 5

我建议您使用 a TimeLinewhere you can specify cycleCountwhich you can set to INDEFINITE.

例子:

Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev -> {
    // TODO do something meaningful here
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
Run Code Online (Sandbox Code Playgroud)