November 8th, 2008
AS3 Redirect/Throw Events Forward and Into Other Files
This is a neat little class I wrote while working on a project with Denis at Jam3.
Basically my part was the UI for the app we were creating and we needed a lot of communication between the UI and the app so this class was one solution to the problem. (a solution that we finally didn’t end up using)
What this class does is listens to events and redirects/dispatches the event again.
Here is the code (download at the bottom):
package com.mikkoh.util { import flash.events.EventDispatcher; /** * This class picks up events and redirects them to whoever is listening * * @author Mikko Haapoja */ public class EventRedirect extends EventDispatcher { private static var instance:EventRedirect; private static var instantiating:Boolean = false; public function EventRedirect():void { if (!instantiating) { throw new Error ("Use getInstance() to get an instance of EventRedirect"); } } public static function getInstance():EventRedirect { if (instance == null) { instantiating = true; instance = new EventRedirect(); } return instance; } public function addRedirect(itemToListenTo:EventDispatcher, event:String):void { itemToListenTo.addEventListener(event, dispatchEvent); } } }