Pro Java 9 Games Development Leveraging the JavaFX APIs

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

You already know why we are using the y-axis for rotation; however, you might be wondering why we
are using only one cycle. The reason is that once we make this RotateTransition interactive by specifying
the fromAngle and toAngle values that will be set before each rotGameBoard.play() method call using code
from the random spin generator that we’ll be coding later, we will control the number of rotations using the
difference between these angles (currently this is 1080 or three spins); therefore, we use only one cycle. I’m
using three spins for code testing purposes.
The rate setting of 1 is too fast to get a smooth spin animation, and game boards shouldn’t spin that
fast, so I reduced this 1.0 default value by 50 percent to 0.5 to show you how the rate variable gives you finely
tuned speed control.
Next, let’s add that required Interpolator class constant specification, which will be the default LINEAR
for now, as we want a smooth, even rotation. This is added and configured using the .setInterpolator()
method call and the Interpolator.LINEAR constant. Finally, we want to add the two most important
configuration statements, which tell the RotateTransition engine the starting angle (fromAngle property)
and the ending angle (toAngle property) for the spin. Using these will allow us to control what quadrant the
spin starts on (45, 135, 225, or 315) and ends on. For now, we will just use three full rotations (1080) from the
starting 45 -degree angle, which will be 1125 for toAngle. To start (and test) the animation, you will also need
a .play() method call, which is shown in the following completed Java method body and shown highlighted
in yellow and light blue at the bottom of Figure 16-4:


RotateTransition rotGameBoard;
...
private void createAnimationAssets() {
rotGameBoard = new RotateTransition(Duration.seconds(5), gameBoard);
rotGameBoard.setAxis(Rotate.Y_AXIS);
rotGameBoard.setCycleCount(1);
rotGameBoard.setRate(0.5);
rotGameBoard.setInterpolator(Interpolator.LINEAR);
rotGameBoard.setFromAngle( 45 );
rotGameBoard.setToAngle( 1125 );
rotGameBoard.play();
}


Figure 16-3. Configure the rotGameBoard RotateTransition object with a y-axis, cycleCount of 1, and rate of
50 percent speed

Free download pdf