简体   繁体   English

使侦听器等待javafx

[英]Making listener wait javafx

I checked some questions here but I couldn't find a proper answer. 我在这里检查了一些问题,但找不到正确的答案。 Maybe I am using wrong keywords to search. 也许我使用错误的关键字进行搜索。 But what I found didn't help. 但是我发现并没有帮助。 So here is my question. 所以这是我的问题。 I have some circles and line that are sliding in a certain way. 我有一些以某种方式滑动的圆和线。 What I want is to update balance variable after the animations perform. 我想要的是在动画执行后更新balance变量。

DoubleProperty balance = new SimpleDoubleProperty();

And I update the balance as follows: 我将余额更新如下:

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);
}

But in main method I want to do something like this 但是在主要方法中,我想做这样的事情

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);
           }
       });

I have some animations to perform before setting scene2 but since balance instantly updating, my animations can't be performed. 设置Scene2之前,我需要执行一些动画,但是由于平衡会立即更新,因此无法执行我的动画。 I want to know if there is a way to delay listening balance or setting scene. 我想知道是否有一种方法可以延迟聆听平衡或设置场景。 I tried Thread.sleep and balance.wait but it gives runtime errors. 我尝试了Thread.sleep和balance.wait,但它给出了运行时错误。

Edit: This question clearly shows that I was lack of knowlodge about javafx. 编辑:这个问题清楚地表明我缺乏关于javafx的知识。 What I want to do is simple and solution is even more simple. 我想做的很简单,解决方案就更简单了。 When animation perform to the end I update the value of balance. 当动画执行到最后时,我会更新平衡值。 All I wanted was make sure that animation is showed to the end. 我想要的只是确保动画显示到最后。

Here is the answer : 这是答案:

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

As Sedrick's comment I change the code like this. 作为Sedrick的评论,我更改了这样的代码。 Adding setOnFinish to animations which need to be performed solve the problem. 将setOnFinish添加到需要执行的动画可以解决该问题。 It's working. 工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM