Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 17 ■ i3D Game Square SeleCtion: uSinG the piCkreSult ClaSS with 3D moDelS


Now use your Run ➤ Project work process to test your code, and you can see that the spinner is not
shown on game start (after you click the Button that hides the uiLayout StackPane Node object) and slowly
and smoothly spins into view in the upper-left corner of the game screen.
The next thing that we need to do is to create a separate rotSpinner Animation object so that we can
have a 3D spinner UI rotation happening at the same time the game board is spinning, for continuity sake.
You will find that if you call rotSpinner.play in your MouseEvent handling construct, you will get an error
as the rotSpinner is part of the spinnerAnim ParallelAnimation object; therefore, we need to duplicate a
rotSpinner construct and create a rotSpinnerIn construct to use in the spinnerAnim ParallelAnimation,
leaving the rotSpinner Animation free for us to call whenever the player randomly spins the game board.
To do this, select all rotSpinner-related Java code, right-click the selection set, and select Copy; then add
a line (space) of code after this code block, right-click, and select Paste to duplicate this code block. Then, all
you have to do is to add “In” at the end of “rotSpinner” and create a rotSpinnerIn code block, which does the
same thing but is not a component of the ParallelTransition construction. Reference the new rotSpinnerIn
Animation object in the object instantiation (constructor method) of the spinnerAnim ParallelTransition
object.
As you can see, the only problem is that your “SPIN” spinner is rotating to the wrong toAngle of 1110 ,
as we coded it in Chapter 16. I’ll set this to -1050 in the next section. The code looks like the following and is
shown in Figure 17-8:


RotateTransition rotGameBoard, rotSpinner;
TranslateTransition moveSpinnerOn;
ParallelTransition spinnerAnim;
...
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);
rotSpinner = new RotateTransition(Duration.seconds(5), spinner);
rotSpinner.setAxis(Rotate.Y_AXIS);
rotSpinner.setCycleCount(1);
rotSpinner.setRate(0.5);
rotSpinner.setInterpolator(Interpolator.LINEAR);
rotSpinner.setFromAngle(30);
rotSpinner.setToAngle(-1110);
rotSpinnerIn = new RotateTransition(Duration.seconds(5), spinner);
rotSpinnerIn.setAxis(Rotate.Y_AXIS);
rotSpinnerIn.setCycleCount(1);
rotSpinnerIn.setRate(0.5);
rotSpinnerIn.setInterpolator(Interpolator.LINEAR);
rotSpinnerIn.setFromAngle(30);
rotSpinnerIn.setToAngle(-1110);
moveSpinnerOn = new TranslateTransition(Duration.seconds(5), spinner);
moveSpinnerOn.setByX(150);
moveSpinnerOn.setCycleCount(1);
spinnerAnim = new ParallelTransition(moveSpinnerOn, rotSpinnerIn);
}

Free download pdf