Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 14 ■ 3D MoDel hierarChy Creation: Using priMitives to Create a gaMe BoarD

Now it is time to start constructing the SceneGraph hierarchy underneath the 3D gameBoard Group
branch of the SceneGraph. The gameBoard Group will contain four quadrant Group branches named
Q1 through Q4. Each of these quadrant Group Node objects will contain a Box primitive quadrant (one-
quarter of the game board center) and that quadrant’s attached five game board squares. The q1 through
q4 quadrant planar objects will also be Box primitives that are four times (300x300) the size of a game board
square.
I am going to move the gameBoard Group object instantiation under the root Group
instantiation and then add the Q1 through Q4 Group object instantiations under that at the top of the
createBoardGameNodes() method so that the Java code order reflects the parent-child hierarchy. Your leaf
objects (bottommost nodes) will be created inside of your createGameBoardNodes() method, including the
q1 through q4 quadrant planar objects, which are leaf nodes of the Q1 through Q4 Group (branch) nodes.
If you like, you can use the handy copy-and-paste programmer’s trick and type in the first Q1 Group
object’s instantiation statement and then copy and paste it three more times underneath itself, changing Q1
to be Q2 through Q4, since at this point, we are just creating four empty quadrant group nodes, which we will
reference to a gameBoard Group node above them and to the Q1S1 through Q4S5 (and q1 through q4) leaf
nodes below them. The resulting Java code should look like the following, as highlighted in yellow, red, and
blue at the top of Figure 14-6:


gameBoard = new Group();
Q1 = new Group();
Q2 = new Group();
Q3 = new Group();
Q4 = new Group();


Now we need to remove your Q1S1 leaf node from the gameBoard branch node and replace it with the Q1
through Q4 branch nodes. For the Q1S1 Box primitive to show up when we select Run ➤ Project (render) for
the 3D Scene, you’ll need to create a second “node builder” .getChildren().add() method chain off of Q1 (Q1S1
object’s parent branch) so that the gameBoard node references the Q1 node, which references Q1S1 node.


Figure 14-6. Add four Group branch node object instantiations under your gameBoard Group, named Q1
through Q4

Free download pdf