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


The easiest way to make sure a game board spin ends on a quadrant is to initialize gameBoard rotation
at 45 degrees and use .setByAngle() to rotate by 90 -degree increments (plus three spins) for each if()
evaluation. This gives us 1080 for 0, 1170 for 1, 1260 for 2, and 1350 for 3. The Java if() constructs are shown
in Figure 17-15 and look like this:


if (picked == spinner) {
int spin = random.nextInt( 4 );
if (spin == 0 ) {
rotGameBoard.setByAngle( 1080 ); // Zero degrees plus 1080
}
if (spin == 1 ) {
rotGameBoard.setByAngle( 1170 ); // 1080 plus 90 degrees is 1170
}
if (spin == 2 ) {
rotGameBoard.setByAngle( 1260 ); // 1080 plus 180 degrees is 1260
}
if (spin == 3 ) {
rotGameBoard.setByAngle( 1350 ); // 1080 plus 270 degrees is 1350
}
rotGameBoard.play();
rotSpinner.play();
}


Figure 17-14. Remove the .setFromAngle(45) and .setToAngle(1125) method calls from the rotGameBoard
object code

Free download pdf