[AS2] "Normal" Event Broadcasting for MovieClips

Hey, I’ve just written this piece of code, it might be useful to someone. :slight_smile:

It’s a simple utility to have add/removeEventListener (dispatchEvent should work too) working for all default MovieClip events.

I should get some time and modify the EventDispatcher so it would scope the handler function to the object that called addEventListener to avoid delegating everything, but it shouldn’t be a problem. (BTW is that possible because of copyrights or do I have to write own EventDispatcher?)

import mx.events.EventDispatcher;

class com.metju.utils.General
{
    public static function goPlaces ( mc : MovieClip ) : Void
    {
        EventDispatcher.initialize ( mc );
        
        var handlers : Array = ['onData', 'onDragOut', 'onDragOver', 
                                'onEnterFrame', 'onKeyDown', 'onKeyUp', 
                                'onKillFocus', 'onLoad', 'onMouseDown', 
                                'onMouseMove', 'onMouseUp', 'onPress', 
                                'onRelease', 'onReleaseOutside', 'onRollOut', 
                                'onRollOver', 'onSetFocus', 'onUnload'];
        
        var l : Number = handlers.length;
        
        for ( var i : Number = 0; i < l; i++ ) {
            // handler function
            var s : String = handlers*
            mc[handlers*] = function () {
                // get what function is being called
                for ( var s : String in this )
                    if ( this[s] == arguments.callee )
                        // dispatch the event
                        this['dispatchEvent'] ( { type:s, target:this} )
            }
        }
    }
}

and usage

var mc : MovieClip = createEmptyMovieClip ( 'mc', 1 );

com.metju.utils.General.goPlaces ( mc );

mc.addEventListener ( 'onEnterFrame', lalala );

function lalala ()
{
    trace ( this + ' YAAAAY!' );
}