简体   繁体   中英

Make a JavaFX application wait between an animation

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

Put all the animations into a SequentialTransition , separated by 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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