Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 8 ■ JavaFX 9 SCene Graph hierarChy: a Foundation For Java 9 Game deSiGn

creditButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
System.out.println("Credits");
}
});


As you can see in Figure 8-15, your event-handling code is error-free, and you are ready to run and test
your JavaFXGame.java game application to make sure that your Scene Graph hierarchy is rendering to the
screen and that your Button UI Control objects are handling event processing correctly. Once you make sure
that your Scene Graph is constructed at this high level for your game and your core user interface processing
Java 9 code structures are also in place and working properly, you can proceed, during the next chapter, to
add digital image assets and fine-tune all of the UI element positioning so that you can make everything look
and function correctly at the top level of your game.


As you can see in Figure 8-16, after you duplicate the .setOnAction() event-handling constructs for
each of your Button objects, when you collapse the EventHandler routines using the minus icons at the left
side of the screen (shown circled in red on the left side of Figure 8-16), you’ll have fewer than a dozen lines
of code in the .start() method. Your first line of code will call a method to create Node objects and configure
them, your second line of code will call a method to add these Node objects to your Scene Graph hierarchy,
lines 3 through 6 will configure your Stage object, and lines 7 through 11 will set up your UI Button Control
object event handling. This is relatively compact if you consider the amount of functionality that you are
adding to the top level of your game infrastructure, including creating your top-level (root and branch node)
Scene Graph structure and user interface design elements for your gameplay, instructions, legal disclaimers,
credits, and scoreboard display.


Figure 8-15. Copy the gameButton and helpButton and paste them to create your scoreButton, legalButton,
and creditButton

Free download pdf