Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 23 ■ Completing the gameplay Code and player proofing your event handling

Place this same if(buttonClick == true) before each of the Button.setOnAction() constructs, and
place buttonClick = false; at the end of each Button.setOnAction() event handling construct, as shown in
Figure 23-15 and Figure 23-16.
To turn all of this event handling back on, we need a Let’s Play Again Button and .setOnAction() event
handler.


Let’s Play Again Button: Resetting Player Event Handling


Once a player clicks an answer Button UI object, all game board squares, spinner, and Button UI objects will
be locked! The best way to unlock everything for another round of gameplay is to add a large yellow Let’s
Play Again Button at the middle of your game board (if you need to peek ahead, it’s shown in Figure 23-23)
that the user will click to spin another time to randomly select a new topic and another image to identify. In
this section of the chapter, we will add this Button element to the root of your SceneGraph, develop the code
for the Button, and finish your player proofing.
Let’s set up an infrastructure for an againButton by adding againButton to the compound Button
declaration at the top of the class and then add the againButton to your SceneGraph root using a
.getChildren().addAll() method chain. The Java code needed to do this is shown here, as well as
highlighted in yellow at the top of Figure 23-17:


Button ... a1Button, a2Button, a3Button, a4Button, againButton;
...
root.getChildren().addAll(gameBoard, uiLayout, qaLayout, scoreLayout, spinner, againButton);


Figure 23-16. At the end of each if(buttonClick==true) construct, set buttonClick = false; to turn off the Button
click function

Free download pdf