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

Now we’re ready to get back into <strong>Java</strong> 8 coding. First, we will add code in our InvinciBagel.java class in order to<br />

implement six AudioClip objects, which will reference our audio assets, and later, in the Bagel.java class, where we will<br />

utilize the assets in a new .playAudioClip() method that we will add to our Bagel class’s primary .update() method.<br />

Adding Audio to InvinciBagel.java: Using AudioClip<br />

The first thing that we need to do to implement the AudioClip sound engine is to declare six private AudioClip objects,<br />

using a compound declaration statement at the top of the InvinciBagel.java class. I will name these iSound (stands for<br />

InvinciBagel Sound) zero through five, as shown in Figure 15-11, using the following single line of <strong>Java</strong> code:<br />

private AudioClip iSound0, iSound1, iSound2, iSound3, iSound4, iSound5;<br />

Figure 15-11. Add the private AudioClip compound declaration statement for your iSound0 through iSound5 objects<br />

As you can see in Figure 15-11, you will have to use the Alt-Enter work process, and select the “Add import for<br />

javafx.scene.media.AudioClip” option, and have NetBeans 8 write your AudioClip class import statement for you.<br />

Referencing AudioClip Assets: Using the java.net.URL Class<br />

Unlike Image objects in <strong>Java</strong>FX, which can be referenced using a simple forward slash character and the file name,<br />

digital audio assets are not as simple to reference, and require the use of the URL class, which is part of the java.net<br />

(network) package. The URL class is used to create a URL object, which provides a Uniform Resource Locator (URL)<br />

file reference, which is essentially a “pointer” to a “data resource,” which is usually a new media asset, and in the<br />

case of our <strong>Java</strong> 8 game development, it is a WAVE audio file in our /src folder.<br />

www.it-ebooks.info<br />

333

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

Saved successfully!

Ooh no, something went wrong!