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

Adding Your Audio Asset Loading Method: .loadAudioAssets( )<br />

Create a method call inside of your start() method, as seen in Figure 15-13, called loadAudioAssets(); and place<br />

it in the logical method order right before the loadImageAssets(); method invocation. To get rid of the wavy red<br />

error highlighting, add a private void loadAudioAssets(){} empty method after the createSceneEventHandling()<br />

method. In this way, your digital audio assets will be referenced and loaded into memory right after your KeyEvent<br />

handling is set up, and right before your digital image assets are referenced and loaded into memory.<br />

Figure 15-13. Create the private void .loadAudioAssets() method to hold the AudioClip object instantiation statements<br />

Inside of your .loadAudioAssets() method body, your will have two <strong>Java</strong> code statements, as loading a digital<br />

audio asset is a little bit more involved than referencing digital image assets. First, you will load your first iAudioFile0<br />

URL object, using the getClass().getResource() method chain. This method chain loads the URL (uniform resource<br />

locator) object with the digital audio sample resource that you want to use, and will do this for your Class object (in<br />

this case, this is the InvinciBagel class, since that is the <strong>Java</strong> class which we are writing this code in).<br />

The resource that you are looking to get the URL for goes inside of the .getResource() method call, and uses the<br />

same format as the resource references that you used for your digital image assets, which in this case would be a<br />

“/leftmono.wav” file reference, which is converted by the .getResource() method into a binary URL data format. This<br />

<strong>Java</strong> code can be seen in Figure 15-14, and looks like the following <strong>Java</strong> method body:<br />

private void loadAudioAssets() {<br />

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

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

}<br />

www.it-ebooks.info<br />

335

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

Saved successfully!

Ooh no, something went wrong!