Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 21 ■ Questions and answers: Finishing the setup Methods and digital audio

Now we can load a second cameraAudio AudioClip object with this camera.wav asset and use it in our code.
Since you have already declared the cameraAudio AudioClip at the top of your class, the next step will
be to instantiate it inside your loadAudioAssets() method, after the spinnerAudio AudioClip object and
its instantiation and configuration statements. After we do this, we can again add the play() trigger to your
createSceneProcessing() code.
You will not need to add a stop() method call to your createAnimationAssets() onFinished() event
handler, as the sound is playing only once and expires at around the same time that the Animation object
finishes moving and rotating the camera object. If you want to sync these more closely, use the same
approach that we did for the spinner audio asset and loop a shorter AudioClip and then call a stop() method
inside of the setOnFinished() event handler.
The Java code for the second instantiation is identical to the first (except for the audio asset’s file name)
and looks like the following, which is shown highlighted in light blue and yellow in the middle of Figure 21-39:


cameraAudio = new AudioClip( JavaFXGame.class.getResource("/camera.wav").toExternalForm() );


If you want to add more digital audio sound effects, you can simply mimic one of these AudioClip
objects or the other, for instance, to add audio to the i3D spinner UI element as it comes onto the screen, to
add audio that has to do with the Q&A sessions, or even to add audio that loops as the Start Game button is
waiting to be clicked by the player. So, you might expand this loadAudioAssets() method, as you continue to
develop and refine this pro Java 9 game design.
Camera animation and audio is triggered in a different part of your createSceneProcessing() method
when the player clicks a game board square to select its content for use in a Q&A session. Therefore, instead
of the play() method being called in if(picked == spinner), it is called in if(picked == Q1S1) or one of
the other 19 game board square conditional if() statements. The Java code, shown in Figure 21-40, should
look like the following:


if (picked == Q1S1) {
setupQ1S1gameplay();
cameraAnimIn.play();
cameraAudio.play();
}


Figure 21-39. Add a cameraAudio AudioClip to the loadAudioAssets() method and reference the new camera.
wav asset

Free download pdf