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 />

Figure 17-13. Create a new <strong>Java</strong> class named Treasure.java extends Actor and create Treasure() and update() methods<br />

Using the Treasure Class: Create Treasure Objects in the Game<br />

Now that we have a Treasure.java class, we can create Treasure objects for the game. As you know, this will be done in<br />

the InvinciBagel.java class. Declare package protected (since we’re going to reference them outside of InvinciBagel,<br />

in Bagel.java) Treasure objects, and name them iTR0 and iTR1 (stands for InvinciBagel Treasure). Add iT0 and iT1<br />

Image object declarations to the end of your private Image objects compound declaration. Instantiate the iT0 and iT1<br />

Image objects using the <strong>Java</strong> new keyword, the Image() constructor method, and the treasure1.png and treasure2.png<br />

image assets, respectively. After that, you can create the iTR0 and iTR1 Treasure objects, using the Treasure()<br />

constructor method, SVG path data of 0,0, 0,64, 64,64 and 64,0 and locations of 50,105 and 533,206 respectively. Make<br />

sure that you add these new Treasure object’s spriteFrame ImageView Nodes to the <strong>Java</strong>FX Scene Graph, using the<br />

.getChildren().add() method chain, called off of the root Group object. As you can see in Figure 17-14, the code<br />

is error-free, and we are now ready to test the new Treasure.java class and the <strong>Java</strong> code that we have added to the<br />

InvinciBagel.java class to see if we can get Treasure in the game Scene. Your <strong>Java</strong> code should look like the following:<br />

Treasure iTR0, iTR1; // These Object Declarations go at the top of the InvinciBagel class<br />

private Image iB0, iB1, iB2, iB3, iB4, iB5, iB6, iB7, iB8, iP0, iP1, iT0, iT1;<br />

iT0 = new Image("/treasure1.png", 64, 64, true, false, true); // .loadImageAssets() Method<br />

iT1 = new Image("/treasure2.png", 64, 64, true, false, true);<br />

iTR0 = new Treasure("M0 0 L0 64 64 64 64 0 Z", 50, 105, iT0); // .createGameActors() Method<br />

iTR1 = new Treasure("M0 0 L0 64 64 64 64 0 Z", 533, 206, iT1);<br />

root.getChildren().add(iTR0.spriteFrame);<br />

root.getChildren().add(iTR1.spriteFrame);<br />

404

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

Saved successfully!

Ooh no, something went wrong!