Thursday, February 18, 2010

Here's How To Start and Stop Sounds (Flash AS 3.0)

Here's the .fla file for you to download.
Here's the .as file for you to download. (right-click and save as for best results!)

package
{
// here's where you import the Sound and SoundChannel classes
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
   
    public class Main extends MovieClip
    {
        //these first variables are to hold sounds that have been imported to the Library
        var noo:Noo;
        var waterloo:Waterloo;
        var ooh:Ooh;

        //this creates a variable to contain a SoundChannel. To play a sound, you must have
        //a SoundChannel to play it in.
        var soundChannel:SoundChannel;

       
       
        public function Main()
        {
            //here we load up the sound variables with instances of the Library sounds
            noo = new Noo();
            waterloo = new Waterloo();
            ooh = new Ooh();
            //here we load the soundChannel variable with a new SoundChannel.
            soundChannel = new SoundChannel;
           
            //here are the Event Listeners!
            //buttonRoll is the instance name of the button in my scene.
            buttonRoll.addEventListener(MouseEvent.MOUSE_OVER, onButtonRollOver);
            buttonRoll.addEventListener(MouseEvent.MOUSE_OUT, onButtonRollOut);
            buttonRoll.addEventListener(MouseEvent.CLICK, onButtonRollClick);
        }
           
            //here are the Event Handlers
        function onButtonRollOver(event:MouseEvent):void
        {
            //this starts the waterloo sunset guitar playing in the soundChannel.
            soundChannel = waterloo.play();
           
        }
        function onButtonRollOut(event:MouseEvent):void
        {
            //this stops any previous sounds that have been playing in the soundChannel.
            soundChannel.stop();
            //this plays the annoying noo sound
            soundChannel = noo.play();
            trace("out of this world");
        }
        function onButtonRollClick(event:MouseEvent):void
        {
            soundChannel.stop();
            soundChannel = ooh.play();
            trace("clickin and tickin");
        }
    }
}

1 comment:

Angelina said...

Sweet!!! Can't wait to try it tomorrow!!!