Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


The void .playFrom(Duration time) method call is a convenience method that will play an Animation
from a specific position, as will the void .playFrom(String cuePoint) method call using a cuePoint rather
than a Duration. A void .playFromStart() method call will play an Animation object from its initial position
in a forward direction. A void .setAutoReverse(boolean value) method call can be used to set the value of
the autoReverse property.
The void .setCycleCount(int value) method call can be used to set the value of the cycleCount
property. The protected void .setCycleDuration(Duration value) method call can be used to set the value
of the cycleDuration property. The void .setDelay(Duration value) method call can be used to set the
value of the delay property. The void .setOnFinished(EventHandler value) method call can
be used to set a value of an onFinished property.
The void .setRate(double value) method call can be used to set the value for your Animation object’s
rate property. The protected void .setStatus(Animation.Status value) method call can be used to set the
constant value of the status property.
The void .stop() method call is used to stop the Animation object playback cycle, and it is important
to note that this method call will reset the playback head to its initial starting position so it can be used as a
reset. Next, let’s take a look at another abstract superclass, Transition, a subclass of Animation and used to
create property transitions.


Automated Object Animation: Transition Superclass


The public abstract Transition superclass is kept in the javafx.animation package along with its subclasses,
which are predefined algorithms, for applying different types of property animation without having to use
timelines or animation timers or to set up keyframes. Therefore, the Transition subclasses are the highest
(most advanced) form of Animation classes and are perfectly suited for pro Java 9 game development, as
they allow you to focus your time on the gameplay development rather than reinventing Java animation
code. This is why we are covering these classes for quickly implementing game animation! The Java class
hierarchy for the Transition superclass looks like the following:


java.lang.Object



javafx.animation.Animation
javafx.animation.Transition



The known direct subclasses that can be quickly and effectively implemented to enhance your
Java games development process include RotateTransition, ScaleTransition, and TranslateTransition
to invoke basic 3D object transforms (these can also be used on 2D, Text, and UI elements); and
FadeTransition, FillTransition, StrokeTransition, and PathTransition for working with 2D (i.e., vector)
objects (FadeTransition works with Text, and UI elements as well). There are also two subclasses used
to create compound (or complex) animations, which combine these other types of property transitions
seamlessly. These include the ParallelTransition, which executes property transitions at the same time, and
SequentialTransition, which executes a string of property transitions serially (one after the other). There’s
also a PauseTransition subclass used to introduce “wait states” into complex animation that will allow more
motion realism to be added into the special animation effect you are trying to create.
The abstract Transition superclass contains all the basic functionality that is required by Transition-
based animation. The class offers a framework to define property animation and can be used to define your
own Transition subclasses as well, if you wish. Most of the types of transitions used for games are already
provided (fade, transforms, path, etc.), however, so all you have to do is implement code that has already
been created, debugged, and optimized.
Transition subclasses all require implementation of a method called .interpolate(double). This
method is called in each cycle of the Animation object, so long as the Transition subclass (object) is running.
In addition to the .interpolate() method, any subclass extending Transition will be required to set a duration
for an Animation cycle using the Animation.setCycleDuration(javafx.util.Duration) method call.

Free download pdf