Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

© Wallace Jackson 2017 545
W. Jackson, Pro Java 9 Games Development, https://doi.org/10.1007/978-1-4842-0973-8_22


CHAPTER 22


Scoring Engine: Creating the Score


UI Layout and Scoring the Content


Now that you have your game board square answer selection logic coded and you have sound effects for
your game board spin and camera animation sequence coded, we need to put the other half of the Java code
into place that looks at what answer the user selects (clicks) and updates the scoreboard accordingly. We will
track both correct and incorrect answers and encourage the player in real time using a simple but effective
scoring interface. The work process in this chapter will necessitate that we also create a Score UI pane
(panel) for the right side of our screen, which we will create using a StackPane named scoreLayout and Text
objects whose names also begin with score.
During this chapter, we’ll be implementing a single-player gameplay and scoring model to get your
scoring user interface in place and because a lot of game players will want to play the game against the
content as a learning experience. That said, there will still be a lot of code to write for each Button UI element
that looks at whether the answer is the correct answer; if it is, the code will increment the “Right:” score, and
if it is not, it will increment the “Wrong:” score.
This means you will still need to add several hundred more lines of Java code after you learn how to
implement scoring logic in this chapter. This will score all your answers, which you learned how to put in
place in the previous chapter.
Fortunately, we will use that optimal “code once, then copy, paste, and modify” approach, so there
should not be too much typing involved, like in the previous chapter. The real work will be creating the
answers (Chapter 21 ) and the scoring logic (this chapter) after you have finished learning how to implement
scoring (in the current chapter).
There is also one small bug from the previous chapter that we will fix by moving the .setVisible(false)
call for the Q&A UI panel from your Start Game Button to the JavaFX application start() method startup
sequence, which will initially hide the Q&A UI panel (and later the Score UI panel) on your game’s startup,
rather than on a Button click.


SplashScreen Render Bug: Hide UI Panels on Startup


You may have noticed during the Run (^) ➤ Project test rendering of your game in the previous chapter that
JavaFX was incorrectly rendering part of the Q&A UI pane (panel) above the SplashScreen for your game,
as shown at the top left of Figure 22-1. This Q&A UI panel should really be behind your SplashScreen, as
you’ve designated that rendering order in your addNodesToSceneGraph() method in the root.addChildren.()
addAll() method chain’s Node object parameter list sequence. By adding i3D elements to make your Scene a
3D (or a “hybrid” 2D+3D Scene) entity, this could also be a Z-unit location (position) setting issue. Therefore,
there could be two ways to investigate and fix this minor rendering issue. Since we already have our X, Y, Z
display units set and working effectively for what we want to achieve in the i3D game rendering pipeline,

Free download pdf