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 15 ■ Implementing Game Audio Assets: Using the <strong>Java</strong>FX AudioClip Class Audio Sequencing Engine<br />

Triggering the .playiSound( ) Methods in Bagel.java: The .playAudioClip( ) Method<br />

Now that we have declared, universal resource located (referenced), and instantiated our AudioClip objects, and<br />

created .playiSound() methods that will allow us to trigger each of these digital audio samples from “outside” of the<br />

InvinciBagel.java class, we can go into the Bagel.java class, and write some code that allows us to trigger these audio<br />

objects, to see how well the AudioClip class works. The best way to do this with our existing code is to use the event<br />

handler code we are using to move our motion sprite object to also allow us to trigger one of these sounds for each<br />

of the KeyEvents that we have set up currently for our game. This is why I named these files with the keys that they<br />

will be triggered by. The first thing that we need to put into place in the Bagel.java class, similar to what we did in the<br />

InvinciBagel class, is a method call to a playAudioClip() method inside of the .update() method that references an<br />

empty private void playAudioClip() method. This method call and empty method body can be seen in Figure 15-19.<br />

Figure 15-19. Create an empty .playAudioClip() method in the Bagel.java class and add a call to it inside of .update()<br />

Inside of the playAudioClip() method body, we need to create conditional if() structures, similar to what we did<br />

for the sprite movement that we created in Chapter 12. We’ll match KeyEvent handling (left, right, up, down, w, s) to<br />

audio files that speak each of the keys (AudioClip objects iSound0 through iSound5) via an invinciBagel.playiSound()<br />

object reference and method call inside of the conditional if() statements, seen in Figure 15-20, using this <strong>Java</strong> code:<br />

private void playAudioClip() {<br />

if(invinciBagel.isLeft()) { invinciBagel.playiSound0(); }<br />

if(invinciBagel.isRight()) { invinciBagel.playiSound1(); }<br />

if(invinciBagel.isUp()) { invinciBagel.playiSound2(); }<br />

if(invinciBagel.isDown()) { invinciBagel.playiSound3(); }<br />

if(invinciBagel.iswKey()) { invinciBagel.playiSound4(); }<br />

if(invinciBagel.issKey()) { invinciBagel.playiSound5(); }<br />

}<br />

340<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!