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 you are ready to call this random number generator inside of your spinner logic in your
MouseEvent handling code, which tells your game what to do when the 3D spinner UI is clicked. Obviously,
the first thing to do is to check for NULL to see whether the click was on a 3D Scene element and, if so, to
then see whether it was the 3D spinner that has been clicked on.
If the spinner was clicked, then the first line of code after if(picked==spinner) would be a
.nextInt(bound) method call, with an upper boundary value of 4 (the lower boundary is zero). This gives
us a random result among four quadrants (zero through three, as the upper bound of four is exclusive and
therefore not utilized in the random number pick range), which is what we will need to randomly select
between for the game’s four quadrants.
Add a line of code before your calls to invoke the RotateTransition Animation objects and create a new
int variable named spin, which will hold the result of your random.nextInt(4) method call. Add an equal ( = )
operator and then type random and a period, which will bring up your NetBeans 9 method helper pop-up
selector drop-down.
Select the .nextInt(int bound) (int) option, which is shown in blue in Figure 17-13, and then double-
click it and insert it into your code. Change the default 0 (which turns the random number generator off by
generating zero to zero results) to a 4 to tell the random number generator to generate four integer values at
random, which will give you the four different quadrant results for your player spins. The Java code at this
point should look like the following Java nested if() constructs, which are also shown highlighted in blue
(and also under construction) in Figure 17-13:


if (picked != null) {
if (picked == spinner) {
int spin = random.nextInt(4);
rotGameBoard.play();
rotSpinner.play();
}
}


Figure 17-12. Instantiate the random Random object in the .start() method so that it is loaded into memory
and ready

Free download pdf