- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道可以使用 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/
这是引自 AnimationTimer类文档。 The class AnimationTimer allows to create a timer, that is called in each fr
所以,我正在使用 JavaFX 创建贪吃蛇游戏,但我似乎无法使游戏正确暂停,即它偶尔会暂停,而其他时候,游戏只是忽略暂停。所以,基本上我有一个 Main 类,我在其中初始化所有 GUI 组件,并且它还
JavaFX 的 AnimationTimer 启动时是否在单独的线程上运行? 我在调试器中运行了一个 JavaFX 应用程序,无论有没有 AnimationTimer,在这两种情况下都有 6 个线程
所以,我正在使用 JavaFX 创建贪吃蛇游戏,但我似乎无法使游戏正确暂停,即它偶尔会暂停,而其他时候,游戏只是忽略暂停。所以,基本上我有一个 Main 类,我在其中初始化所有 GUI 组件,并且它还
当玩家赢得我创建的游戏时,我试图显示一个警报对话框。但是,我遇到了一个异常(exception): java.lang.IllegalStateException: showAndWait is no
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我目前正在开发一款使用 JavaFX 制作的游戏,我想知道如何将 AnimationTimer 暂停一定的秒数?我尝试使用 Thread.sleep,但它会导致整个窗口卡住。任何帮助将不胜感激,如果需
我知道可以使用 this.stop 函数从其覆盖的 handle() 方法中停止正在进行的动画。下面的代码来自游戏,在游戏获胜并显示警报后,它应该停止运行动画。相反,警报会弹出无数次。 new Ani
我已经编写了一个简单的游戏,使用 JavaFX 作为 GUI 并使用 AnimationTimer 作为游戏本身,但我发现在 AnimationTimer 对象内更新的任何字段都无法在该对象外部找到。
我将 AnimationTimer 用于多个任务,例如带有不断变化的图片的动画和 ProgressIndicator 动画。为了达到所需的速度,我将线程置于 hibernate 状态,但是当多个动画同
我正在尝试向地球绕太阳运行的小动画添加一个“开始”按钮。我的问题是,当我单击按钮时,AnimationTimer 似乎从未收到 start() 调用。 当单击按钮的事件被触发时,动画计时器应该启动,从
考虑这段代码: import java.util.ArrayDeque; import javafx.animation.AnimationTimer; import javafx.animation
我有一个用 JavaFX 编写的俄罗斯方 block 游戏。我想把它翻译成C++来学习C++。我可以在 C++ 中使用什么来显示图形并为游戏循环计时,就像我使用 JavaFX 和 AnimationT
我正在尝试制作一个游戏,其中我有类矩形的障碍,并让它左右滑动。然而,虽然看起来应该可以工作,但我命名为障碍物 1 的矩形却没有任何移动。 import javafx.animation.Animati
我是一名优秀的程序员,十分优秀!