Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


The ParallelTransition class has only one native property, an ObjectProperty node property,
which is the Node object to which the combined animation will be applied. If a node isn’t specified, the child
Animation object node property will be utilized instead. If a node is specified, that Node will be set (i.e.,
specified) for all child Transitions that do not themselves define any target Node node property.
The ParallelTransition class contains four overloaded constructor methods. The first creates an empty
object, the second specifies a list of child Animation objects, the third specifies a Node to be affected, and
the fourth specifies both the Node object to be affected and a list of child Animation objects. The second and
fourth constructor methods are the most frequently used. We will be using the second constructor for our child
Animation objects; both referenced Animation Transition objects specify the spinner Node object as the target
for the ParallelAnimation object. The Java code for these constructor methods looks like the following:


parallelTransition = new ParallelTransition();
parallelTransition = new ParallelTransition(Animation... children);
parallelTransition = new ParallelTransition(Node node);
parallelTransition = new ParallelTransition(Node node, Animation... children);


The ParallelTransition class has only about a half-dozen method calls that you will need to master. The
.getChildren() method call will return an ObservableListof Animation objects that are to be
played together as a single, unified animation.
The .getNode() method call can be used to get (poll) the Node object value of the node property, and
the void .setNode(Node value) method call can be used to set (specify) the Node object value of the node
property.
There is also a protected Node .getParentTargetNode() method call that will return the target Node for
child Animation objects for the Transition that do not have a node property specified. To specify a parent
target node property, the fourth constructor method, which specifies a node property for ParallelTransition
(the parent), must be utilized. Otherwise, the second constructor method would be used, and the child
Animation object’s node property will define what Node object the Animation object will be affecting.
The ParallelTransition .nodeProperty() method call will return your ParallelTransition (parent)
ObjectProperty value, which would be set using the third or fourth constructor method or
.setNode(Node). This Node, if specified (set), will be used in all child Transitions that do not specifically
define their target Node.
Finally, the protected void .interpolate(double value) method call is required to be provided by all
subclass implementations of the abstract Transition superclass.
Next, let’s set up a ParallelTransition object that seamlessly combines your rotSpinner and
moveSpinnerOn Animation objects together.


ParallelTransition Object: Merge rotSpinner and moveSpinnerOn


Let’s add a ParallelTransition Animation object to your game project by declaring one named spinnerAnim
at the top of your class and then instantiate it inside of the createAnimationAssets() method, after
the RotateTransition and the TranslateTransition Java code. In the constructor method, reference the
moveSpinnerOn and rotSpinner Animation child objects and then call the .play() method off the
spinnerAnim object. Notice I have commented out the rotSpinner.play() method call and did not add
a .play() method call to the moveSpinnerOn Animation object, as this is done in the spinnerAnim
ParallelTransition object instead. The setup for this parallel (hybrid) animation would be accomplished
using the following Java statements, which are also highlighted in yellow and blue in Figure 16-10:


ParallelTransition spinnerAnin;
...
spinnerAnin = new ParallelTransition(moveSpinnerOn, rotSpinner);
spinnerAnim.play();

Free download pdf