<< Chapter < Page Chapter >> Page >

Play an occasional sizzle sound

Listing 8 uses a similar process to play an occasional sizzle sound. Listing 8 also causes the sizzle sound to be followed immediately by a clap of thunder.

Play an occasional sizzle sound.

if ((loopCntr % 35 == 0)&&(Math.random()>0.5)&&(sizzlePlaying == false)) { //Don't play another sizzle sound until this one// finishes. sizzlePlaying = true;//Play the sizzle sound and get a reference to the// SoundChannel object through which it is being // played.channel = sizzle.play();//Register an event listener that will be called // when the sizzle sound finishes playing.channel.addEventListener( Event.SOUND_COMPLETE, soundCompleteHandler);}//end if}//end onTimer

Save the SoundChannel reference

Listing 8 saves the SoundChannel reference returned by the play method in an instance variable named channel when the play method is called to play the sizzle sound.

Don't corrupt the reference to the SoundChannel object

In order to preclude the possibility of corrupting this reference by changing its value while the sound is playing, Listing 8 uses a Boolean instance variable named sizzlePlaying to guarantee that a new sizzle sound is not played before the previous one finishes.

The value of sizzlePlaying is set to true when the sizzle sound starts playing in Listing 8 and is set to false later when the sizzlesound finishes playing. Because the value of sizzlePlaying is tested by the conditional clause in the if statement in Listing 8, that conditional clause will never return true while sizzlePlaying is true.

Register a SOUND_COMPLETE event handler

The SoundChannel object fires a SOUND_COMPLETE event when the sound that it is playing finishes. Listing 8 registers an eventlistener on the SoundChannel object that is called each time the sizzle sound finishes playing. As you will see shortly, the code in theevent handler sets the value of sizzlePlaying to false and also causes the thunder sound to be played as soon as the sizzle sound finishes.

The SOUND_COMPLETE event handler

The SOUND_COMPLETE event handler is shown in its entirety in Listing 9. This method is called each time the sizzle sound finishes playing.

The sound_complete event handler.

private function soundCompleteHandler(e:Event):void { //Allow another sizzle sound to be played now that// this one is finished. sizzlePlaying = false;//Play the thunder immediately following the end of // the sizzle sound.thunder.play(); }//end soundCompleteHandler//--------------------------------------------------//} //end class } //end package

Allow another sizzle sound to be played

Listing 9 begins by setting the value of sizzlePlaying to false. This makes it possible for the sizzle sound to be played again when theother two expressions in the conditional clause of the if statement in Listing 8 return true.

Play a thunder clap

Then Listing 9 calls the play method on the thunder sound to cause the thunder sound to be played once immediately following thecompletion of each sizzle sound.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask