28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 ■ The Foundation of Game Design: The <strong>Java</strong>FX Scene Graph and the InvinciBagel Game Infrastructure<br />

Scene Graph Design: Streamlining the Existing .start() Method<br />

Now, you can optimize the .start() method so that it only has one or two dozen lines of code. First, modularize the<br />

Scene Graph node creation <strong>Java</strong> routines into their own createSplashScreenNodes() method, which will be called<br />

at the top of the .start() method, as can be seen in Figure 6-5. After all the nodes are created in this method, create an<br />

addNodesToStackPane() method to add the nodes to the StackPane root node, and then have the three primaryStage<br />

lines of code configuring and managing the Stage object and, finally, the ActionEvent handling code routines that<br />

“wire” the Button UI controls to the <strong>Java</strong> code to be executed when they are clicked.<br />

Figure 6-5. Organize the .start() method with the createSplashScreenNodes() and addNodesToStackPane() methods<br />

As you can see, after you duplicate the .setOnAction() constructs for each Button object, when you collapse<br />

the EventHandler routines, you have nine lines of code: one for creating nodes, one for adding nodes to root, three<br />

for Stage object setup, and four for UI Button event-handling. This is pretty compact, if you consider the amount<br />

of functionality you are adding to the top level of your game structure (game play, instructions, legal, credits,<br />

scoreboard).<br />

It is important that you do things in the correct order, as some <strong>Java</strong> code is predicated on other <strong>Java</strong> code. For this<br />

reason, the object declarations come first; then, inside the .start() method, you create (instantiate) the nodes. Once<br />

these are declared, named, and instantiated (created), you add them to the StackPane root node and then configure<br />

(using the .setTitle() method) and add the scene Scene object to the primaryStage Stage object, using the .setScene()<br />

method. After your objects are in system memory, only then will you be able to process ActionEvent handling<br />

routines, which are attached to your four Button UI controls. Next, let’s make sure that your digital image assets, which<br />

will be referenced in the createSplashScreenNodes() method, are in the proper NetBeans folder.<br />

128

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!