Events are the core of actions in Flash. Often, though, the Events provided fail to provide the some information or command needed.

Say, for instance, a you have a number of sound files loading and playing, and want to manage them within another class. You want to listen for events that happen to the Sounds as well as the SoundChannels, the Event.SoundComplete event, and not have to have a complicated way of getting to the Sound in question. For that, you can set up a new Event to handle Sound events from various sources.

In the example below, I have taken a standard Event and added the ability to pass a Sound object with the event. I have also added a few more event types (EA_AUDIO_ADDED, etc.). I would have to set up where and when to fire the new event types; however, up to this point, I did not have access to some of these functions at all.

To use, set up a dispatchEvent within the sound loader class, the sound playing class, or wherever you are getting a sound from. For example, within a class called (for example) MyOwnSoundPlayer:

dispatchEvent(new EventAudio_test(someAudioObject, EventAudio_test.EA_SOUND_PLAY_COMPLETE, false, false));

Then you set up a listener attached to that class instance (this would be in another class, probably, though it doesn’t have to be – a class can listen for it’s own events, too), which references a function somewhere (usually this is in the same class, but an event listener can reference any accessible method or function):

var plyr:MyOwnSoundPlayer = new MyOwnSoundPlayer();
plyr.addEventListener(EventAudio_test.EA_SOUND_PLAY_COMPLETE, functionToRun, false, 0, true);

And the called function would look something like this:

protected function functionToRun(e:EventAudio_test):void
{
    var audioRef:Sound = e.audio;
    // Sound work here using audioRef . . .
}

So here is the full EventAudio_test class:

package AS3_0.classes.Events // Put your own package here.
{
    // Load whatever you need.  This is set up for sound files:
    import flash.media.Sound;
    import flash.events.Event; // Import the Event class.

    public class EventAudio_test extends Event // Extend Event to get it's functionality
    {
        // Create a bunch of global String constants for events that you need.
        // The constant's name should indicate the usage.
        // I also like to have identical text as the name, so I can't make a stupid mistake.
        static public const EA_AUDIO_ADDED:String = "EA_AUDIO_ADDED";
        static public const EA_SOUND_LOADED:String = "EA_SOUND_LOADED";
        static public const EA_ALL_CHANNELS_CLEARED:String = "EA_ALL_CHANNELS_CLEARED";
        static public const EA_SOUND_PLAY_COMPLETE:String = "EA_SOUND_PLAY_COMPLETE";

        // This is a new variable that holds whatever you want passed within the event through it's parameters.
        // An Event is only a function, so you can have as many variables as you wish, and can pass Functions, Arrays, Objects, etc.
        private var _audio:Sound;

        public function EventAudio(audio:Sound, type:String, bubbles:Boolean = false, cancelable:Boolean = false)
        {
            // Store the reference:
            this.audio = audio;
            // Finish the Event constructor:
            super (type, bubbles, cancelable);
        }

        public override function clone():Event
        {
            // Note that the variable needs to be added to the clone() method:
            return new EventAudio(audio, type, bubbles, cancelable);
        }

        public override function toString():String
        {
            // Note that the variable needs to be added to the toString() method:
            return formatToString("audio", "type", "bubbles", "cancelable", "eventPhase");
        }

        // Allow public access to the variable via getter/setter methods:
        public function get audio():Sound { return _audio; }
        public function set audio(value:Sound):void
        {
            // Not necessary, but is always good to reduce duplication, especially if you start using extensive objects:
            if (_audio !== value)
            {
                _audio = value;
            }
        }
    }
}

– 30 –

Advertisements