views:

212

answers:

2

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{}
+1  A: 

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.

Matthew Hegarty
Unless i have mistunderstood (which happens all the time), I don't use any subtimelines in my snippet. Isn't subtimelines timelines inside keyframes in the original timeline? And as far as i can see, those parts of the code you posted on JIRA shouldn't really affect the time altering anyway. The time bound to a changing variable seems to be the problem still. I couldn't get it to work when moving the declaration either. Did you move it outside the whole script?
Vargen
I was hoping that the bug report would give you some pointers rather than be a direct solution. To move forward, is there any way you can try using the *Transition classes instead? I did find this area to be rather buggy when I tried, so maybe you could try raising in JIRA if you still have no success?
Matthew Hegarty
A: 

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.

Eric Wendelin