Let's look at a code sample where the user's mouse click triggers a gunshot. The click also triggers related attack actions, such as identifying the target and determining the damage, which we already covered in previous chapters.
This is how you play a gunshot sound after a click:
Make a copy of
Main.java
and name the classAudioTrigger.java
. Remember to also refactor the first line of themain()
method toAudioTrigger app = new AudioTrigger();
.Copy the provided file
Sounds/Effects/Gun.wav
into your project'sSounds/Effects/
directory.Create a private class field for the audio node:
private AudioNode gunAudio;
Initialize the audio node in the
simpleInitApp()
method. You want it to play theGun.wav
sound. It's a short sound, so we use the default constructor that pre-buffers the audio:public void simpleInitApp() { gunAudio = new AudioNode( assetManager,"Sounds/Effects/Gun.wav");
Declare an input mapping for a gunshot: map the left mouse button...