gpt4 book ai didi

java - 让 JavaFX 应用程序在动画之间等待

转载 作者:行者123 更新时间:2023-12-01 16:31:36 25 4
gpt4 key购买 nike

我正在使用 JavaFX 开发一个简单的游戏。我想要的是在循环结束时,应用程序等待指定的时间,然后再次运行。

当我在应用程序线程上运行此代码时, View 不会更新,节点也会消失,而我看不到动画。

如果我创建一个新线程,那么什么也不会发生。由于该动画在游戏完成之前不会运行,因此在动画完成之前是否其他任何操作都没有关系。

下面是我的代码,感谢任何帮助。

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

最佳答案

将所有动画放入 SequentialTransition 中,用 PauseTransition 分隔:

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

关于java - 让 JavaFX 应用程序在动画之间等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62030436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com