EventHandlers: Difference between revisions
(Created page with "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 c...") |
Whirled vox (talk | contribs) m (Text replacement - ">" to ">") |
||
| (One intermediate revision by the same user not shown) | |||
| Line 3: | Line 3: | ||
== Code == | == Code == | ||
Code courtesy of Zefyr. | Code courtesy of Zefyr. | ||
<actionscript> | |||
package { | package { | ||
| Line 21: | Line 21: | ||
{ | { | ||
dispatcher.removeEventListener(event, listener); | dispatcher.removeEventListener(event, listener); | ||
for (var ii :int = 0; ii | for (var ii :int = 0; ii < _eventHandlers.length; ii++) { | ||
if (dispatcher == _eventHandlers[ii].dispatcher && event == _eventHandlers[ii].event && | if (dispatcher == _eventHandlers[ii].dispatcher && event == _eventHandlers[ii].event && | ||
listener == _eventHandlers[ii].func) { | listener == _eventHandlers[ii].func) { | ||
| Line 41: | Line 41: | ||
} | } | ||
} | } | ||
</actionscript> | |||
== Example Usage == | == Example Usage == | ||
Instead of the normal addEventListener function call, use a line similar to the following, replacing Event.ENTER_FRAME and onEnterFrame as needed. | Instead of the normal addEventListener function call, use a line similar to the following, replacing Event.ENTER_FRAME and onEnterFrame as needed. | ||
<actionscript> | |||
EventHandlers.registerEventListener(this, Event.ENTER_FRAME, onEnterFrame); | EventHandlers.registerEventListener(this, Event.ENTER_FRAME, onEnterFrame); | ||
</actionscript> | |||
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. | 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. | ||
<actionscript> | |||
public function handleUnload() : void { | public function handleUnload() : void { | ||
EventHandlers.unregisterEventListener(this, Event.ENTER_FRAME, onEnterFrame); | EventHandlers.unregisterEventListener(this, Event.ENTER_FRAME, onEnterFrame); | ||
} | } | ||
</actionscript> | |||
In your main class's handleUnload function, you can call this instead: | In your main class's handleUnload function, you can call this instead: | ||
<actionscript> | |||
EventHandlers.freeAllHandlers(); | EventHandlers.freeAllHandlers(); | ||
</actionscript> | |||
This will free all event listeners that were registered with EventHandlers. | This will free all event listeners that were registered with EventHandlers. | ||
Latest revision as of 05:36, 3 September 2018
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. <actionscript>
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 = [];
}
}
</actionscript>
Example Usage
Instead of the normal addEventListener function call, use a line similar to the following, replacing Event.ENTER_FRAME and onEnterFrame as needed. <actionscript>
EventHandlers.registerEventListener(this, Event.ENTER_FRAME, onEnterFrame);
</actionscript>
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.
<actionscript>
public function handleUnload() : void {
EventHandlers.unregisterEventListener(this, Event.ENTER_FRAME, onEnterFrame);
}
</actionscript>
In your main class's handleUnload function, you can call this instead:
<actionscript>
EventHandlers.freeAllHandlers();
</actionscript>
This will free all event listeners that were registered with EventHandlers.