Pro Java 9 Games Development Leveraging the JavaFX APIs

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

Triggering Spinner Audio Playback in createSceneProcessing()


To play the AudioClip object, we need to insert a spinnerAudio.play(); method call into the event handling
for your if (picked == spinner) structure near the end, right before the calculateQuadrantLanding()
method call.
The Java 9 code for this addition is shown highlighted at the bottom of Figure 21-36.


To stop your spinnerAudio AudioClip object’s playback, you will need to call your stop() method off of
the spinnerAudio AudioClip inside of the setOnFinished() event handling code structure, which is called off
of the rotGameBoard Animation object inside of the createAnimationAssets() method body.
In this way, when the Animation object is finished animating, your spinnerAudio.stop() method is
called, and the game board spin audio stops when the game board stops spinning.
I placed this code at the very end of the event handling structure using the following code, which is
highlighted in light blue and yellow at the end of Figure 21-37:


rotGameBoard.setOnFinished(event-> {
if (quadrantLanding == 315) { populateQuadrantOne(); }
if (quadrantLanding == 225) { populateQuadrantTwo(); }
if (quadrantLanding == 135) { populateQuadrantThree(); }
if (quadrantLanding == 45) { populateQuadrantFour(); }
spinnerAudio.stop();
});


Figure 21-36. Trigger a spinnerAudio.play() in the createSceneProcessing() method in an if(picked ==
spinner) structure

Free download pdf