简体   繁体   English

使 JavaFX 应用程序在动画之间等待

[英]Make a JavaFX application wait between an animation

I am working on a simple game using JavaFX.我正在使用 JavaFX 开发一个简单的游戏。 What I want here is that at the end of the loop, the application waits for a specified period of time, and then runs again.我在这里想要的是,在循环结束时,应用程序等待指定的时间段,然后再次运行。

When I run this code on the application thread, the view doesn't get updated and the Nodes disappear without me seeing the animation.当我在应用程序线程上运行此代码时,视图不会更新并且节点消失而我看不到动画。

If I create a new thread, then nothing happens at all.如果我创建一个新线程,则什么也不会发生。 Since this animation isn't run until the game has been completed, it doesn't matter if nothing else works until the animation is completed.由于此动画在游戏完成之前不会运行,因此在动画完成之前,如果没有其他任何工作,则无关紧要。

Below is my code and any help is appreciated.下面是我的代码,任何帮助表示赞赏。

private void playWonAnimation(){
    Random rand = new Random();
    for (Node block: tower02List) {
        double xTrans = rand.nextInt(800) + 700;
        double yTrans = rand.nextInt(800) + 700;

        TranslateTransition translate = new TranslateTransition(Duration.millis(2500), block);
        xTrans = (xTrans > 1100) ? xTrans : -xTrans;
        translate.setByX(xTrans);
        translate.setByY(-yTrans);

        RotateTransition rotate = new RotateTransition(Duration.millis(1200), block);
        rotate.setByAngle(360);
        rotate.setCycleCount(Transition.INDEFINITE);

        ParallelTransition seq = new ParallelTransition(translate, rotate);
        seq.setCycleCount(1);
        seq.play();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Put all the animations into a SequentialTransition , separated by PauseTransition s:将所有动画放入一个SequentialTransition ,由PauseTransition s 分隔:

private void playWonAnimation(){
    Random rand = new Random();
    SequentialTransition seq = new SequentialTransition();
    for (Node block: tower02List) {
        double xTrans = rand.nextInt(800) + 700;
        double yTrans = rand.nextInt(800) + 700;

        int translateTime = 2500 ;
        int oneRotationTime = 1200 ;

        TranslateTransition translate = new TranslateTransition(Duration.millis(translateTime), block);
        xTrans = (xTrans > 1100) ? xTrans : -xTrans;
        translate.setByX(xTrans);
        translate.setByY(-yTrans);

        RotateTransition rotate = new RotateTransition(Duration.millis(translateTime), block);
        rotate.setByAngle(360 * translateTime / oneRotationTime);

        seq.getChildren().add(new ParallelTransition(translate, rotate));
        seq.getChildren().add(new PauseTransition(Duration.seconds(1.0)));
    }

    seq.play();
}

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

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