gpt4 book ai didi

java - AnimationTimer stop()方法不停止

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:17 24 4
gpt4 key购买 nike

我知道可以使用 this.stop 函数从其覆盖的 handle() 方法中停止正在进行的动画。下面的代码来自游戏,在游戏获胜并显示警报后,它应该停止运行动画。相反,警报会弹出无数次。

new AnimationTimer() {
@Override
public void handle(long now) {
if (game.isInProgress()) {
nextFrame();
} else {
if (game.isWon()) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Game Over!");
alert.setHeaderText("Congratulations, you have won the game!");
alert.setContentText(toolbarModule + ": " + game.getLevel().getScoreObject().getScore()
+ ", Time Remaining: " + toolbarModule.getTimeString() + "\n Rank: "
+ toolbarModule.getRankString());
alert.show();
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Game Over!");
alert.setHeaderText("Sadly, you have lost the game!");
alert.setContentText("Time passed: " + game.getLevel().getTimeLimit());
alert.show();
}
new EndScene(game, stage);
this.stop();
}
}
}.start();

我做错了什么?

任何帮助将不胜感激,

最佳答案

我想通了。实际上是合乎逻辑的(通常是这样)。 alert.show 函数等待用户确认弹出窗口。同时,handle() 方法多次运行该方法。我需要的是运行这个弹出窗口一次。我通过将警报包含在一个条件中来做到这一点。像这样:

      if (!victoryMessageReceived) {
victoryMessageReceived = true;
if (game.isWon()) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Game Over!");
alert.setHeaderText("Congratulations, you have won the game!");
alert.setContentText(toolbarModule + ": "
+ game.getLevel().getScoreObject().getScore() + ", Time Remaining: "
+ toolbarModule.getTimeString() + "\n Rank: " + toolbarModule.getRankString());
alert.show();
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Game Over!");
alert.setHeaderText("Sadly, you have lost the game!");
alert.setContentText("Time passed: " + game.getLevel().getTimeLimit());
alert.show();
}
}

感谢大家的努力!

关于java - AnimationTimer stop()方法不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46677277/

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