28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

The next step in the work process is to “wire” the scoreText Text object into the scoreFont Font object using the<br />

.setFont() method, which is called off of the scoreText object and sets the Text object to utilize the scoreFont Font<br />

object using the following code, which is shown in the bottom part of Figure 17-5. You can also use a .setFill() method<br />

called off of the scoreText Text object to set the color of the Text object; for now, we will use a Color.BLACK constant.<br />

scoreText.setFont(scoreFont);<br />

scoreText.setFill(Color.BLACK);<br />

Now we can test the scoreText Text object placement, and refine our X,Y screen location values, so that the score<br />

is right next to the HBox UI Button bank. I found that I had to move the Y location down twenty pixels to a pixel<br />

location of 385, while I had to move the X location five pixels to the right, to a pixel location of 445, as is shown in<br />

Figure 17-6, in the left half of the screen shot, in the bottom right of the InvinciBagel game, next to the Legal Button.<br />

Figure 17-6. Test the scoreText and scoreFont objects using the Run ➤ Project work process to refine their placement<br />

We will be adding a Text object label that says “SCORE:” in the next section of the chapter to label our score for<br />

the player. The result of this second round of coding is shown in the right half of Figure 17-6, at the bottom right.<br />

Creating a SCORE Label: Adding the Second Text Object<br />

Instead of just having a number on the right side of the UI Button bank (HBox), let’s add a Text label that says SCORE:<br />

to the front of the scoreText Text object. We will create a scoreLabel Text object and use the same Font object that<br />

we created in the previous section to style that Text object. I am going to change the numeric part of the score text to<br />

be blue, using the scoreText.setFill(Color.BLUE); <strong>Java</strong> statement, and make the SCORE: label black, using the<br />

following <strong>Java</strong> code, which can also be seen in Figure 17-7:<br />

scoreText.setFill(Color.BLUE);<br />

scoreLabel = new Text();<br />

scoreLabel.setText("SCORE:");<br />

scoreLabel.setLayoutY(385);<br />

scoreLabel.setLayoutX(445);<br />

scoreLabel.setFont(scoreFont);<br />

scoreLabel.setFill(Color.BLACK);<br />

www.it-ebooks.info<br />

397

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!