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

Before we code this spin logic, we need to remove the .setFromAngle() and .setToAngle() method calls
from your rotGameBoard block of statements in the createAnimationAssets() method, which simplifies
your rotGameBoard Animation object logic down to its five required statements (instantiation, axis, cycles,
rate, and interpolator). We will also do this later for your rotSpinner, after we have ascertained that a switch
from toAngle and fromAngle to byAngle is going to work correctly for generating ongoing game board spins
using the fewest lines of code and with zero errors.
What we are doing here is using createAnimationAssets() to create and configure the Animation
objects and then using a .setByAngle() in the if() conditional statements, which evaluate the Random
random object result, placed into the spin integer, which we will be doing next. This approach will also
reduce the amount of code in this method body as well to less than two dozen lines of code (unless we add
game board animation later during the design and development process, outlined within this book). The
rotGameBoard code, shown in Figure 17-14, now looks like this:


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);
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(-1050);
...
}


Figure 17-13. Add an int variable named spin and then type random and a period and select nextInt(int
bound) set to 4

Free download pdf