28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Figure 17-12. Make all the previously unrelated if() structures into one if-else-if stucture by inserting else in between ifs<br />

Adding Bounty to the Game: The Treasure.java Class<br />

To make our game play more exciting, let’s add a Treasure.java class, so that our game players have something to look<br />

for during game play that allows them to add points to their score now that the scoring engine is in place. This type<br />

of Treasure object will take advantage of the hasValu and isBonus boolean flags, which we installed in the abstract<br />

Actor class, and we will set these to a true value in the Treasure() constructor method, which we will code next, along<br />

with overriding the required .update() method, so that later on in development we can add animation and treasure<br />

processing logic. Like the Prop, PropV, PropH, and PropB classes, this class will use the xLocation and yLocation<br />

parameters to set the translateX and translateY properties for the spriteFrame ImageView Node object, which will<br />

live inside of this Treasure object (Actor object type, also Actor subclass) as part of the Treasure() constructor method<br />

programming logic. The <strong>Java</strong> code for this class, which can also be seen in Figure 17-13, should look like the following:<br />

package invincibagel;<br />

import javafx.scene.image.Image;<br />

public class Treasure extends Actor {<br />

public Treasure(String SVGdata, double xLocation, double yLocation, Image... spriteCels){<br />

super(SVGdata, xLocation, yLocation, spriteCels);<br />

spriteFrame.setTranslateX(xLocation);<br />

spriteFrame.setTranslateY(yLocation);<br />

hasValu = true;<br />

isBonus = true;<br />

}<br />

@Override<br />

public void update() { // Currently this is an Empty but Implemented Method }<br />

}<br />

www.it-ebooks.info<br />

403

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

Saved successfully!

Ooh no, something went wrong!