Pro Java 9 Games Development Leveraging the JavaFX APIs

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

So, logically, we have connected the click “protection” for the game board squares to the game board
spin Animation object. Also, note that by initializing all of the click protection variables as boolean with no
(default) values, we have essentially set all click protection to false, or “click is locked,” simply by declaring
these variables at the top of your Java class. In this way, we do not need any clickProtect = false;
statements in your start() method body.
Notice in Figure 23-11 that I have also declared the spinnerClick and buttonClick using a compound
Java (boolean) declaration at the top of your class. This is because we want to “lock” the i3D spinner UI
element, the game board squares, and the Q&A Button UI elements once the player has clicked them. This
is to prevent multiple clicks, which prevents multiple answer button clicks (to run up the score). It also
assures that your i3D animations and audio calls are triggered only once per game round as needed to
prevent what will look (or sound) like bugs to your player. You don’t really want an animation to restart in
the middle of its intended visual result, even though it will if you tell it to (soon enough in its playback cycle,
which is what more than one click will usually do), so we’ll lock the clicks after one!
Next, let’s set up locking for the spinnerClick, starting with the if(picked == spinner) conditional
evaluation in the MouseEvent handling code. We need to add && spinnerClick == true to the if(picked ==
spinner) to evaluate whether we are allowed to click the spinner at that point in the gameplay. If we are, we
immediately set spinnerClick to a false value since the spinner Animation object (and quadrant-landing
processing) is also started within this block of code. We’ll enable clicking the spinner in the .setOnFinished()
handler for the spinner after it has finished spinning, which will prevent a player from being able to click
your i3D spinner UI element while it is actually spinning! Cool!
This new mouse-click-proofing addition to your i3D spinner’s conditional if() Java code infrastructure is
shown in bold here, as well as highlighted in light blue and yellow at the top of Figure 23-12:


if(picked == spinner && spinnerClick == true) {
spinnerClick = false;
...
}


Next, we’ll add a .setOnFinished() event handler for this rotSpinner Animation object, which sets
the boolean spinnerClick variable to a false value once the rotSpinner Animation object has finished. The
reason this is false, which turns the click off, is because we don’t want the spinner (or the game board) to
spin again until the player has chosen a square and a corresponding answer Button UI element to register
(and score) their answer.


Figure 23-12. Add && spinnerClick == true to the if() evaluation for the spinner in your MouseEvent handling
structure

Free download pdf