//SOUND CONTROL Example // Shows two methods to let the user turn music on and off // Method 1: Have two buttons, on for turning the music on // and one for turning the music off // Method 2: Have one on/off button: // It turns the music on if it's currently off // It turns the music off if it's currently on package { import flash.display.*; import flash.events.*; import flash.media.SoundChannel; public class Wk9MusicButton extends MovieClip { // Create an instance of a custom sound class that // you created and set up a "linkage" var gameMusic: MyMusic = new MyMusic( ); // Create a SoundChannel object to control the music var gameMusicControl:SoundChannel = new SoundChannel(); // Boolean variable used by the on/off music button // to keep track of whether the music currently is on or off var soundOn: Boolean; function Wk9MusicButton( ) { // Button listeners for two buttons, one that turns the music on and one off onBtn.addEventListener( MouseEvent.CLICK, turnMusicOn ); offBtn.addEventListener( MouseEvent.CLICK, turnMusicOff ); // Button listener for on/off button that turns the music on and off musicBtn.addEventListener(MouseEvent.CLICK, switchMusicStatus); } function turnMusicOn( evt: MouseEvent ) { gameMusicControl = gameMusic.play( ); } function turnMusicOff( evt: MouseEvent ) { gameMusicControl.stop( ); } // Button handler that stops the music if it's on and starts it if it's off function switchMusicStatus( evt:MouseEvent ) { if (soundOn == true) // Stop music { gameMusicControl.stop(); soundOn = false; msgBox.text = "Off"; } else // Start music { gameMusicControl = gameMusic.play( ); soundOn = true; msgBox.text = "On"; } } } }