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

Be sure not to call a invinciBagel.scoreText.setText(String.valueOf(invinciBagel.gameScore));<br />

statement, which updates the scoreText UI element using a .setText() method, inside of each of these conditional if()<br />

statements. Instead, notice that I am placing it at the end of the method, after all of these evaluations are completed.<br />

I am doing this so I only have to call this one line of code one time, at the end of the conditional if() object processing<br />

structure. That said, the <strong>Java</strong> code will still work if you place this scoreText object score update statement inside each<br />

of the conditional if() statement code body structures. However, I’m trying to show you how to write code that does a<br />

lot of work by using relative few (a dozen or less) lines of code to accomplish major <strong>Java</strong> 8 game development tasks.<br />

Next, we are going to take the .playiSound0() method call out of the .checkCollision() method, and we’ll put it<br />

inside of the scoringEngine() method instead. As long as it is in one of these two methods, it’s going to get called on<br />

collision, one way or the other. The reason that I am going to do it this way is because we will probably want to play a<br />

different sound effect when a player finds a treasure, or is hit by a projectile. In this way, your audio is associated with<br />

scoring and game play when different types of collisions are detected, rather than just playing audio for any collision.<br />

Our conditional if() structure code bodies use curly braces, which allows us to add <strong>Java</strong> statements into each type<br />

of collision (scoring) object instance (type). So in addition to incrementing (or decrementing, as we will add later) the<br />

score, we can also use different sounds (audioClips) for different objects. Let’s add the .playSound() method calls into<br />

these conditional if() code blocks next, so that we have the code in place to trigger sound effects when a treasure is<br />

picked up by the primary character or when he is hit by (or catches) a projectile shot by an enemy (an InvinciBeagle)<br />

character, or when he collides with a prop in the scene.<br />

This is done by using the following <strong>Java</strong> conditional if() structures, which can also be seen in Figure 17-10:<br />

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

if(object instanceof Prop) {<br />

invinciBagel.gameScore+=5;<br />

invinciBagel.playiSound0();<br />

}<br />

if(object instanceof PropV) {<br />

invinciBagel.gameScore+=4;<br />

invinciBagel.playiSound1();<br />

}<br />

if(object instanceof PropH) {<br />

invinciBagel.gameScore+=3;<br />

invinciBagel.playiSound2();<br />

}<br />

if(object instanceof PropB) {<br />

invinciBagel.gameScore+=2;<br />

invinciBagel.playiSound3();<br />

}<br />

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

}<br />

400<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!