EventHandlers

Here is a simple way to keep track of your project's event handlers, and unload all of them at once. This is particularly needed for ENTER_FRAME listeners.

Code
Code courtesy of Zefyr. package { import flash.events.IEventDispatcher; public class EventHandlers {     public static function registerEventListener (dispatcher :IEventDispatcher, event :String,          listener :Function) :void {         dispatcher.addEventListener(event, listener); _eventHandlers.push({dispatcher: dispatcher, event: event, func: listener}); }     public static function unregisterEventListener (dispatcher :IEventDispatcher, event :String,          listener :Function) :void {         dispatcher.removeEventListener(event, listener); for (var ii :int = 0; ii < _eventHandlers.length; ii++) { if (dispatcher == _eventHandlers[ii].dispatcher && event == _eventHandlers[ii].event &&                 listener == _eventHandlers[ii].func) { _eventHandlers.splice(ii, 1); break; }         }      }      public static function freeAllHandlers  :void {         for each (var handler :Object in _eventHandlers) { handler.dispatcher.removeEventListener(handler.event, handler.func); }         _eventHandlers = []; }     protected static var _eventHandlers :Array = []; } }

Example Usage
Instead of the normal addEventListener function call, use a line similar to the following, replacing Event.ENTER_FRAME and onEnterFrame as needed. EventHandlers.registerEventListener(this, Event.ENTER_FRAME, onEnterFrame);

Add a handleUnload function to your class, if you do not already have one, customizing as needed to unregister each listener that you previously registered for that class. Call the handleUnload function when the object that is listening no longer needs to listen.

public function handleUnload : void { EventHandlers.unregisterEventListener(this, Event.ENTER_FRAME, onEnterFrame); }

In your main class's handleUnload function, you can call this instead:

EventHandlers.freeAllHandlers;

This will free all event listeners that were registered with EventHandlers.