# \[AS2\] "Normal" Event Broadcasting for MovieClips

**URL:** <https://forum.kirupa.com/t/as2-normal-event-broadcasting-for-movieclips/236594>\
**Category:** open source\
**Created:** [August 22, 2007, 2:57am UTC](https://forum.kirupa.com/t/as2-normal-event-broadcasting-for-movieclips/236594 "2007-08-22T02:57:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [August 22, 2007, 2:57am UTC](https://forum.kirupa.com/t/as2-normal-event-broadcasting-for-movieclips/236594/1 "2007-08-22T02:57:35Z")

</div>

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

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?)

```auto
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

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

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

mc.addEventListener ( 'onEnterFrame', lalala );

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

```
