Hello, everyone,
I am trying to animate movement of a rectangular sprite across the screen using keyboard controls, but I notice subtle pauses in the animation initially. There are typically one to three pauses, jerks or jitters. Then the rest of the time, the animation is generally smooth.
Here is a gif showing the movement.
Gyfcat Link
In the below code, I use keybindings to set boolean flags for the direction keys. Then in an animation timer, I check the flag, set the sprite's velocity and set the sprite's layout property. I restrict movement to one axis at a time. I do diagonal movement by alternating movement in each axis.
Please try this code.
In this post (
How to get smooth animation with KeyPress event in javaFX?), the answer was varying the sprite speed with the animation timer's time argument, but even when I vary the speed using the timer, there is still pausing.
In this post (
In JavaFX how do I move a sprite across the screen?), the answer moves the sprite by the position properties instead of the layout properties, but that did not resolve this pausing.
In addition, I have tried using setOnKeyPressed and setOnKeyReleased, but there was no effect on the problem.
I considered that there may be video card or memory problems with the computer, so I tried this code on two computers and the problem exists on both computers. Both computers ran the code in Windows 10 and the Eclipse
IDE.
Similarly, when I run the code here, (
Key Combinations), I also see these kinds of pauses in the animation, so I am worried that it might not just be my code.
Please help me resolve these subtle pauses in animating a rectangular sprite using keybindings.
Thank you very much for taking a look at my problem.
P.S. I have also posted this question at stackexchange (
Pauses in Sprite Animation when Using Keybindings in JavaFX).