Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 22 ■ SCoring engine: Creating the SCore Ui LayoUt and SCoring the Content

Now we’re ready to add the score engine logic to the Q&A Button elements we put into place in Chapter 21
and add Text answerRight and answerWrong options to our Score UI design. This will display the score generated
by the Q&A UI Button elements. After that, we can calculate the grade and assign it to a seventh Text element for a
letter grade.


Scoring Engine: Logic to Calculate Score on Answer


Let’s add a custom method to hold our scoring engine logic called createQAprocessing(), which will hold
the .setOnAction(event) event processing for the four Button elements created in the createQAnodes()
method. As you can see in Figure 22-21, this needs to come after we set up the Q&A and Score UI
designs in createQAnodes() and createScoreNodes() and before we call this Q&A event handling in
createSceneProcessing(). So, add a line of Java code after the scoreLayout.setVisible(false); statement and
before the createSceneProcessing(); statement, as shown highlighted in yellow in Figure 22-21. Use the
Alt+Enter work process to remove the wavy red error underlining by having NetBeans 9 write the bootstrap
method code and error statement, which we will be replacing soon. This will open a drop-down menu
containing the Create method “createQAprocessing()” in javafxgame.JavaFXGame option, which you will
double-click to execute (a single-click will select this single option, as shown in Figure 22-21).


The easiest way to code this first (of four) action event handling structure is to go into your .start()
method and copy and paste one of your Button event handling structures that you created earlier in the
book into this newly created createQAprocessing() method. Be sure to select the NetBeans bootstrap error
statement line of code completely before you use the Paste command so that your ActionEvent handling
code replaces this bootstrap error statement.


Figure 22-21. Add the createQAprocessing() method call after createQAnodes() and createScoreNodes() and
use Alt+Enter

Free download pdf