Make a JavaFX application wait between an animation

自闭症网瘾萝莉.ら 提交于 2020-06-09 05:24:06

问题


I am working on a simple game using 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();
        }
    }
}

回答1:


Put all the animations into a SequentialTransition, separated by PauseTransitions:

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


来源:https://stackoverflow.com/questions/35923464/make-a-javafx-application-wait-between-an-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!