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

Since we want to instantiate (create) our random number generator “engine” before it is actually
utilized, let’s have our code instantiate (create and load into system memory) the Random number generator
when the game application starts up.
This dictates that we will place the Random() constructor method code in your .start() method, right
before your ActionEvent handling constructs and right after all of your Node and Scene and Stage objects
have been created and added to the SceneGraph. For good measure, we’ll put it after all of the custom
methods that create the assets, images, animation and eventually the digital audio samples and other new
media assets that we’ll be using to create a professional Java 9 game.
We can do this because this Random object (named random) is not utilized until the player has clicked
the Start Game Button object to enter the 3D Scene and then clicked the spinner 3D UI (Sphere) element.
Thus, you can put this Random object instantiation anywhere in your .start() method, from the first line of
code to the last, as long as this object is created (loaded into system memory) before you start generating
any MouseEvent handling method calls in your custom createSceneProcessing() method, which we will
be enhancing as we proceed through this chapter. Open your .start() method body in NetBeans 9, add a
line of code after your custom method calls, and instantiate your Random object named random using the
following Java code, which is also shown in Figure 17-12:


public void start(Stage primaryStage) {
... // Custom Methods Up Here
random = new Random();
... // ActionEvent Handling Constructs Down Here
}


Figure 17-11. Declare a Random object named random at the top of class; use Alt+Enter to add import
java.util.Random

Free download pdf