在 JavaFX 中动态更改关键帧时间属性

发布于 2024-08-13 08:31:55 字数 634 浏览 9 评论 0原文

我正在 JavaFX 中制作游戏,并实现了一个滑块来控制游戏速度。我有一个简单的滑块(javafx.scene.control.Slider),我将游戏循环的时间属性绑定到从滑块获得的值。

滑块仅在提高游戏速度时才真正起作用。如果我减少它,gameUpdate() 将停止一段时间 - 取决于我减少它的程度。如果我在等待游戏赶上时增加滑块,游戏将再次继续。有时,无论我等多久,游戏似乎根本跟不上。

一般来说,更改关键帧时间是一个坏主意还是我忘记了其他事情? 我一直在尝试更改 canSkip 变量,这似乎可以让游戏再次启动时运行得更流畅,但对我没有多大帮助。

def gameLoop:Timeline = Timeline{
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame{
            time: bind Duration.valueOf(Config.REFRESH_RATE_NUMBER - gameSpeed)
            action: function(){
                gameUpdate();
            }
        }
    ]//keyFrames[]
}// Timeline{}

I am making a game in JavaFX and have implemented a slider for controlling the game speed. I have a simple slider (javafx.scene.control.Slider) and I'm binding the time attribute for the gameloop to the value I get from the slider.

The slider only really works when increasing the gamespeed. If i decrease it, the gameUpdate() will stop for a while - dependent on how much i decease it. If i increase the slider while waiting for the game to catch up, the game will continue again. Sometimes the game doesn't seem to catch up at all no matter how long i wait.

Is changing the keyframe time a bad idea in general or am i forgetting something else?
I have been trying out changing the canSkip variable, and that seems to get the game running smoother when it starts again, but does not help me much.

def gameLoop:Timeline = Timeline{
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame{
            time: bind Duration.valueOf(Config.REFRESH_RATE_NUMBER - gameSpeed)
            action: function(){
                gameUpdate();
            }
        }
    ]//keyFrames[]
}// Timeline{}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-08-20 08:31:55

我见过与此相当类似的情况(尽管在 JavaFX 1.1.1 中),我在 JIRA

我发现如果我将声明从脚本级别移至初始化函数(而不是 init{} 块)中,问题就得到了解决。

不过我同意动态更改关键帧时间是一个坏主意。
显然不支持在您发布的代码片段中使用“子时间线”,建议的解决方案是使用 JavaFX 1.2 SequentialTransitionParallelTransition 时间线。

阅读 JIRA 错误报告以获取更多信息,如果不能解决问题,请回复。

I have seen a situation rather similar to this (although in JavaFX 1.1.1), which I reported in JIRA

I found that this was solved if I moved the declaration from script level - in my case this was into an initialisation function (not init{} block).

However I would agree that changing the keyframe time dynamically is a bad idea.
The use of "subtimelines" in the snippet you have posted is apparently not supported, and instead the recommended solution is to use the JavaFX 1.2 SequentialTransition and ParallelTransition timelines.

Read the JIRA bug report for more info and please post back if it doesn't solve the problem.

微凉徒眸意 2024-08-20 08:31:55

我建议在函数中生成一系列关键帧,然后删除并重新设置关键帧。通过绑定,它似乎可能会在用户拖动滑块时尝试调整时间线。

I would suggest generating a sequence of keyframes in a function, then deleting and re-setting the keyframes. With the bind it seems it might try to adjust the timeline while the user is dragging the slider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文