Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 16 ■ 3D Game animation Creation: UsinG the animation transition Classes


The abstract Animation class cannot directly create Animation objects, but it does provide
core functionality across all animation classes used in the JavaFX API. The exception to this is the
AnimationTimer class (a pulse engine), which implements a core timer pulse engine (thus, it is more of a
timer class than an animation class) and is ideal for 2D sprite-based games. I get into this class in Beginning
Java 8 Games Development, where I cover i2D games development in detail. I am focusing more on i3D
games development in this book, so I will take the opportunity to cover some of the other useful (which are
also canned or precoded) animation transition classes.
The animation can run a finite number of times by setting the cycleCount property. To make any
animation “pong” (i.e., run back and forth from start to end to start again), set the autoReverse property to
true; otherwise, use a false boolean value, which we’ll be using in our pro Java 9 game to randomly spin the
i3D game board in one direction.
To play an Animation object once you have instantiated and configured it, you call the play() method
or the playFromStart() method. The Animation object’s currentRate property sets your speed and
the direction. By inverting the numeric value of currentRate, you can toggle your play direction. Your
animation will stop whenever the duration property has been “satisfied” (exhausted, come to an end, been
expended, been reached, expired, and so forth).
You can set an indefinite duration (sometimes called a loop or infinite loop) for the Animation object by
using a cycleCount property with an INDEFINITE constant. Animation objects configured in this fashion
run repeatedly until a stop() method is called. This will stop a running Animation and reset its playback
to its starting point (property settings). An Animation can also be paused by calling pause(), and the next
play() call will resume an Animation from where it was paused, unless you use a .playFromStart() method
call. Let’s take a look at the properties that are part of the Animation superclass next. These are inherited by
the Transition superclass, and all of its subclasses, so you will be using these over the course of the rest of
this book in your Pro Java 9 Games Development code.
The autoReverse BooleanProperty is used to define whether an Animation object is supposed to
reverse its direction on alternating cycles. currentRate is a ReadOnlyDoubleProperty that is used to indicate
current speed (and direction, indicated by positive or negative value) at which an Animation object’s other
settings are being played.
A currentTime ReadOnlyObjectProperty is used to define an Animation object playback
position, and a cycleCount IntegerProperty is used to define the number of cycles to play an Animation
object. A cycleDuration ReadOnlyObjectProperty is a read-only variable that can be used to
indicate the duration of one cycle of the Animation object. This is the time it takes to play from time 0 to the
end of the Animation, at the default rate of 1.
The delay ObjectProperty is used to delay the start time for your animation, and the
onFinished ObjectProperty<EventHandler> property contains the ActionEvent to be
triggered at the conclusion of the Animation object playback. The rate DoubleProperty is used to define the
speed and the direction at which your Animation is targeted to be played at. Note that because of hardware
limitations, this rate may not always be possible, so there is a currentRate property to hold the actual
achieved playback rate.
The status ReadOnlyObjectProperty<Animation.Status> property contains the enum status constant for
the Animation object. The Enum Animation.Status helper class holds three constants: PAU S E D, RUNNING,
and STOPPED.
The totalDuration ReadOnlyObjectProperty property holds a read-only variable to
indicate the total duration of the Animation object, which is multiplied by the cycleCount property to factor
in how many times it repeats. So, duration is one cycle, and totalDuration is equal to (delay + (duration *
cycleCount) ).
Animation has one static (nested) class, which is an Animation.Status class that holds Enum constants
representing the possible states for status. These include PAU S E D, RUNNING, and STOPPED.
Animation has one data field, the static int INDEFINITE field, which is used to specify an animation
that will repeat itself indefinitely until the .stop() method is called.

Free download pdf