Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 16 ■ 3D Game animation Creation: UsinG the animation transition Classes

This duration should be set using the duration property (as in RotateTransition.duration), for example.
But it can also be calculated by the extending class as is done in ParallelTransition and FadeTransition.
The Transition class has one interpolator property, of type ObjectProperty, which
is used to control the timing for the acceleration and deceleration of each Transition cycle. Properties
inherited from Animation superclass include the autoReverse, currentRate, currentTime, cycleCount,
cycleDuration, delay, onFinished, status, rate, and totalDuration properties. There’s also an Animation.
Status nested class, inherited from the Animation class.
There are two overloaded Constructors; one constructs an empty transition subclass object, and
the other one constructs a frame-rate-configured transition subclass. These look like the following two
constructor methods:


Transition()
Transition(double targetFramerate)


Finally, there are six methods added to this abstract superclass, most of which are related to
the interpolate property. This class also inherits the methods we covered in the previous section. A
.getCachedInterpolator() method returns the Interpolator property that was set when the Transition
subclass was started. The .getInterpolator() method will get the value of the interpolator property, while the
void .setInterpolator(Interpolator value) method will set a value for the interpolator property. As mentioned,
the protected abstract void .interpolate(double) method needs to be provided by Transition subclasses, and
the .interpolatorProperty() method controls your timing for acceleration and deceleration.
Finally, the .getParentTargetNode() method call will return the Node that has been targeted to play
the animation for the Transition subclass. Next, let’s take a look at one of these Transition subclasses in
detail, and then we can implement it in your JavaFXGame Java code to rotate (animate the rotation of ) your
gameBoard Group Node.


Animating 3D Object Rotation: Using the RotateTransition Class


The public final RotateTransition class will be used to create rotation animation and extends the Transition
superclass. It will be stored in the javafx.animation package, with all the other animation and animation
timer-related classes. The Java class hierarchy for the RotateAnimation subclass should look like the
following Java class hierarchy:


java.lang.Object



javafx.animation.Animation
javafx.animation.Transition
javafx.animation.RotateTransition



A RotateTransition class (object) can be used to create a rotation animation that lasts as long as its
duration setting. This is done by updating the rotate variable of the Node it is attached to at a regular
interval. A rotation angle value should be specified using degrees. Rotation starts from a fromAngle property,
if provided, or else it will proceed from the node’s current (previous) rotation value. Rotation will stop using
the toAngle value, if provided, or else it will use a start value plus the byAngle value. The toAngle value will
take precedence if both the toAngle and the byAngle have been specified.
The RotateTransition adds properties to those inherited from Animation and Transition, which help to
define the rotation transition algorithm. These include an axis ObjectProperty property that is
used to specify an axis of rotation for the RotateTransition object; a node ObjectProperty property,
which is used to specify your target Node object that should be affected by the RotateTransition; and
duration ObjectProperty property, which is used to specify the duration for the RotateTransition.

Free download pdf