Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 7 ■ IntroduCtIon to JavaFX 9: overvIew oF the JavaFX new MedIa engIne

JavaFX Animation for Games: Using javafx.animation Classes


The javafx.animation package contains the Animation superclass and Timeline, AnimationTimer,
Interpolator, KeyFrame, and KeyValue classes. It also contains the Transition superclass and ten
transition subclasses, all of which we are going to take a look at during this section of the chapter, as
animation is an important design element for pro Java 9 games development. Since these animation classes
are already coded for us, thanks to the JavaFX 9 API, all that we have to do to add animation to games is
use these classes properly. You’ll be spending a lot of time with these classes, so I’m going to go into each
one in detail so you know how each one works, which ones work together, and which ones you’ll need to
implement your own Java 9 game logic solution.


The JavaFX Animation Class: A Foundation for Animation Objects in JavaFX


The Animation class or more accurately the Animation object, provides the core functionality for animation
in JavaFX. The Animation class contains two (overloaded) Animation() constructor methods. They include
Animation() and Animation(double targetFramerate), and they will create the Animation object in
memory, which will control your animation, and its playback characteristics and life cycle, from a high-level
object that contains other subobjects.
The Animation class contains the .play() method, the .playFrom(cuePoint) or .playFrom(Duration
time) method, and a .playFromStart() method. These methods are used to start playback for the Animation
object. There is also the .pause() method, which can pause the animation playback, and a .stop() method,
which can stop animation playback. There are .jumpTo(Duration time) and .jumpTo(cuePoint) methods
to jump to predefined positions in an animation.
You can set the animation playback speed (some call this the frame rate, or FPS) by using the rate
property. The cycleCount property (variable) allows you to specify how many times an animation will loop,
and the delay property allows you to specify a delay time before the animation starts. If your animation is
looping, this delay property would specify your delay time used between loops, which can be used to create
some realistic effects.
You can specify a seamless animation loop by setting the cycleCount attribute or property (variable)
to be INDEFINITE and then using the autoReverse property (set to false), or you can use pong (back
and forth) animation looping by specifying the true value for the autoReverse property. You can also set
cycleCount to a numeric value such as 1 if you want the animation to play only one time and not loop
indefinitely.
There is a .setRate() method to set the animation playback rate property, a .setDelay() method to set
the delay property, and .setCycleCount() and .setCycleDuration() methods for controlling the cycling
characteristics. As you might imagine, there are also similar .get() methods to “get” the currently set values
for these Animation objects variables (or properties, attributes, parameters, characteristics; however you
prefer to look at your data fields is fine).
You can assign an action to be executed when the animation has completed playback using the
onFinished property, loaded with an ActionEvent object. This will be executed when the animation reaches
the end of each loop, and as you can imagine, some very powerful things can be triggered in a pro Java game
using this particular capability.
There are read-only variables (properties) that you can “poll” at any time, to find the status,
currentTime, currentRate, cycleDuration, and totalDuration for each Animation object. For instance, you
can use this currentTime property to see the position of the playback head (frame pointer) at any point in
time in the animation playback cycle.

Free download pdf