我在这里检查了一些问题,但找不到正确的答案。也许我使用错误的关键字进行搜索。但是我发现并没有帮助。所以这是我的问题。我有一些以某种方式滑动的圆和线。
我想要的是在动画执行后更新balance变量。

DoubleProperty balance = new SimpleDoubleProperty();


我将余额更新如下:

if (shapeADone == false) {
    intersecting = arc.centerXProperty().lessThan(boldLine.getEndX() - 13);
    intersecting.addListener((obs, wasIntersecting, isNowIntersecting) -> {
        System.out.println("Collision!");
        animation1.stop();
        animation2.stop();
        animation3.stop();
    });
    again = true;
    balance.setValue(-1);
} else {
    fadeB();
    balance.setValue(1);
}


但是在主要方法中,我想做这样的事情

level1.balance.addListener(ov -> {
           System.out.println("The new value is " +
                   level1.balance.doubleValue());
           if (level1.balance.getValue()==1) {
              //delay setting scene
               primaryStage.setScene(scene2);
           }
       });


设置Scene2之前,我需要执行一些动画,但是由于平衡会立即更新,因此无法执行我的动画。
我想知道是否有一种方法可以延迟聆听平衡或设置场景。
我尝试了Thread.sleep和balance.wait,但它给出了运行时错误。

编辑:这个问题清楚地表明我缺乏关于javafx的知识。我想做的很简单,解决方案就更简单了。当动画执行到最后时,我会更新平衡值。我只想确保动画显示到最后。

最佳答案

这是答案:

else{
                animation3.setOnFinished(event -> balance.setValue(1));
                fadeB();
            }


作为Sedrick的评论,我更改了这样的代码。
将setOnFinish添加到需要执行的动画即可解决该问题。工作正常

09-10 02:31
查看更多