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

Adding Treasure Collision Detection: Updating .scoringEngine()<br />

Change your Prop Actor objects scoring to reduce the scoreboard value by changing the += values to -=1 or -=2, and<br />

then add an else-if{} section at the end of the existing if-else-if chain, to support Treasure objects, which we will score<br />

at +=5, to add five points. The <strong>Java</strong> code to do this, which you can see in Figure 17-16, should look like the following:<br />

private void scoringEngine(Actor object) {<br />

if(object instanceof Prop) {<br />

invinciBagel.gameScore-=1;<br />

invinciBagel.playiSound0();<br />

} else if(object instanceof PropV) {<br />

invinciBagel.gameScore-=2;<br />

invinciBagel.playiSound1();<br />

} else if(object instanceof PropH) {<br />

invinciBagel.gameScore-=1;<br />

invinciBagel.playiSound2();<br />

} else if(object instanceof PropB) {<br />

invinciBagel.gameScore-=2;<br />

invinciBagel.playiSound3();<br />

} else if(object instanceof Treasure) {<br />

invinciBagel.gameScore+=5;<br />

invinciBagel.playiSound4(); }<br />

invinciBagel.scoreText.setText(String.valueOf(invinciBagel.gameScore));<br />

}<br />

Figure 17-16. Add a Treasure else-if structure to end of the .scoringEngine() method conditional if() structure<br />

406<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!