- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我正在使用 JavaFX 创建贪吃蛇游戏,但我似乎无法使游戏正确暂停,即它偶尔会暂停,而其他时候,游戏只是忽略暂停。所以,基本上我有一个 Main
类,我在其中初始化所有 GUI 组件,并且它还充当 javafx 应用程序的 Controller 。
我有一个名为 gameControl
的 Button
来启动/暂停游戏,还有一个变量 Boolean Pause
用来跟踪游戏状态(新/paused/running),以及方法 startGame
、pauseGame
。
gameControl
按钮的EventHandler
如下:
gameControl.setOnClicked(event->{
if(paused == null) startGame(); //new game
else if(paused) continueGame(); //for paused game
else pauseGame(); //for running game
});
startGame
函数看起来像这样:
void startGame(){
paused = false;
Snake snake = new Snake(); //the snake sprite
//following gameLoop controls the animation of the snake
gameLoop = new AnimationTimer(){
@Override
public void handle(long now){
drawSnake(); //draws the snake on the game
snake.move(); //move snake ahead
//following code is for slowing down the gameLoop renders to make it easier to play
Task<Void> sleeper = new Task<>(){
@Override
protected Void call() throws Exception {
gameLoop.stop();
Thread.sleep(30);
gameLoop.start();
return null;
}
};
new Thread(sleeper).start();
//force garbage collection or else throws a bunch of exceptions after a while of running.
//not sure of the cause...
System.gc();
}
};
gameLoop.start();
}
AnimationTimer gameLoop
是类的变量,允许从其他函数调用。
以及pauseGame
函数:
void pauseGame() {
paused = true;
gameLoop.stop();
}
所以,正如我之前所说,每次我点击 gameControl
按钮时游戏都不会暂停,我怀疑这是由于 Thread.sleep(30);
code> 行位于 gameLoop
的 Task
内。话虽这么说,我仍然不完全确定,也不知道如何解决这个问题。任何帮助将不胜感激。
最佳答案
“暂停”是什么类型?您检查它是否为空,然后将其视为 boolean 值。我不明白为什么它会是一个大的“B” boolean 对象包装器而不是原始 boolean 类型。
这个:
//following code is for slowing down the gameLoop renders to make it easier to play
Task<Void> sleeper = new Task<>(){
@Override
protected Void call() throws Exception {
gameLoop.stop();
Thread.sleep(30);
gameLoop.start();
return null;
}
};
这是一种绝对可怕的限制速度的方法。让你的游戏循环运行,检查每个循环的时间,看看是否已经过去了足够的时间来更新内容。您的动画计时器将驱动游戏。您不想暂停主平台线程,也不想暂停任何正在处理任务的工作线程。如果您正在安排任务,请将它们安排为按照您想要的时间间隔运行 - 不要在 call() 方法中限制线程。
你真正想要的是这样的:
//following gameLoop controls the animation of the snake
gameLoop = new AnimationTimer(){
@Override
public void handle(long now){
if ((now - lastTime) > updateIterval) {
drawSnake(); //draws the snake on the game
snake.move(); //move snake ahead
lastTime = now;
}
您甚至可以将其设置为一个循环来“ catch ”,以防动画计时器由于某种原因落后:
while ((now - lastTime) > updateIterval) {
drawSnake(); //draws the snake on the game
snake.move(); //move snake ahead
lastTime += updateIterval;
}
关于JavaFX AnimationTimer 未正确停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65061944/
这是引自 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
我是一名优秀的程序员,十分优秀!