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

Figure 15-14. Instantiate and load the URL object, and then use it inside of the iSound0 AudioClip object instantiation<br />

The second line of code, interestingly, uses the .toString() method call, which converts this URL object right back<br />

into a String object, which is required to load the AudioClip() constructor method with the audio asset URL. You may<br />

be thinking: why not use iSound0 = new AudioClip("/leftmono"); ? You can try this, however, you’ll have to “hard<br />

code” the directory, using “file:/Users/users/MyDocuments/NetBeansProjects/InvinciBagel/src/leftmono.wav.”<br />

I used the URL object approach so that you will be able to reference this audio file from the inside of the JAR file,<br />

instead of using the approach shown above that requires an “absolute” location on a hard disk drive. Therefore, this<br />

getClass().getResource() method chain is adding “relative” reference data into this URL object. Your InvinciBagel<br />

class needs this relative reference data to be able to reference your WAV audio resource files from the inside of your<br />

NetBeans 8 project InvinciBagel/src folder, as well as from the inside of your JAR file for your <strong>Java</strong> 8 game application.<br />

Next, use your trusty programmer’s shortcut, and copy and paste these two lines of code five more times<br />

underneath themselves, which is the easy way to create your other five AudioClip objects. Change the zero on both<br />

iAudioFile0 and iSound0 to iAudioFile1 through iAudioFile5 and iSound1 through iSound5 respectively. Then change<br />

the WAV audio file name reference to rightmono.wav, upmono.wav, downmono.wav, wmono.wav and smono.wav<br />

respectively. Your completed loadAudioAssets() method should then look like the following <strong>Java</strong> method body, which<br />

is also shown in Figure 15-15:<br />

private void loadAudioAssets() {<br />

iAudioFile0 = getClass().getResource("/leftmono.wav");<br />

iSound0 = new AudioClip(iAudioFile0.toString());<br />

iAudioFile1 = getClass().getResource("/rightmono.wav");<br />

iSound1 = new AudioClip(iAudioFile1.toString());<br />

iAudioFile2 = getClass().getResource("/upmono.wav");<br />

iSound2 = new AudioClip(iAudioFile2.toString());<br />

336<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!