English 中文(简体)
Dynamically changing a keyframes time attribute in JavaFX
原标题:

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{}
问题回答

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.

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.





相关问题
JavaFX with Maven [closed]

I recently started a JavaFX project, and I d like to use Maven as my compiler/deployment tool. Is there a good tutorial or plugin to integrate JavaFX and Maven?

Displaying a string with variables in JavaFX

I have little quiz game I ve working on and am tracking correct answers. I have a text object that displays the number of consecutive correct answers. This is the content: content: bind consecutive....

non java-developer question

Just a basic question about Java (haven t really done anything with it personally yet): Is it possible to write a Java program that runs in a web browser (via JRE) on the client machine? Is ...

Pros and Cons of JavaFX and Silverlight [closed]

I know there is already a question about the performance of Flex, JavaFX, and Silverlight. My question is a bit more broad: We are evaluating the merits of JavaFX and Silverlight to serve as the GUI ...

How to simplify SVG code?

Is it possible to simplify / clean up svg code by replacing the use-tags with standard svg elements? Maybe an inkscape plugin? Haven t found anything... Background: I m converting some svgs to javafx ...

Dynamically changing a keyframes time attribute in JavaFX

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 ...

Migrate Java Applet to what/where?

I am reviewing currently a medium size code base (around 30K LOC) which uses a huge Applet and interfaces with other systems. It s a tool to create custom labels, so we need drag-n-drop and other ...

热门标签