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);
    }
  }
}

The meat of this Class is all in the function addRedirect.

public function addRedirect(itemToListenTo:EventDispatcher, event:String):void
{
  itemToListenTo.addEventListener(event, dispatchEvent);
}

You pass this function anything that can dispatch events (everything that dispatches events extends EventDispatcher ie. Sprite, MovieClip, Stage) and a String for the event you want the event dispatcher to listen to (ie. MouseEvent.CLICK, Event.ENTER_FRAME). This function then adds an event listener to the itemToListenTo that just dispatches/redirects the event. I took a bit of a shortcut and made the dispatchEvent function listen to the events being sent from itemToListenTo instead of writing another function that would then dispatch the event anyway.

The rest of this class is just a Singleton implementation. For you who do not know what a Singleton is, a Singleton is a type of class that you can only have one of. (I know I always thought what can I even use that for) I did this to ensure that everywhere where this class is used you will have the same instance. For example my Ui was another swf being loaded into Denis’ app and I was registering to redirect events to Denis’ app so we needed to make sure that Denis got the exact same instance to listen to.

So in short I will explain how you actually use this class.

Within the class you want to dispatch events (ie. a button):

//get an instance
var redirect:EventRedirect=EventRedirect.getInstance();
 
//this is this button and MouseEvent.CLICK is
//the event we want to dispatch everywhere
redirect.addRedirect(this, MouseEvent.CLICK);

In classes we want to listen to events:

//get an instance
var redirect:EventRedirect=EventRedirect.getInstance();
 
//doSomething is the function that will pick up the
//events being dispatched from btn
redirect.addEventListener(MouseEvent.CLICK, doSomething);

Now this class is very powerful but also very dangerous which is why we didn’t end up using this in our project. I could see this class being very useful for smaller projects but could be very scary for very large projects. On a large project you could have 100’s of events being redirected everywhere and so it may get very annoying sorting out who is passing a MouseEvent.CLICK event and what should be done. The other option would be to create very specific user defined events and I think this would be a better option. Something also to keep in mind is that the more events a class is listening to the slower it will be. So if the re-directer is listening to 100’s of events it may become a bottle neck within your app.

Click here to download the source and a silly example use of this class


No Comments, Comment or Ping

Reply to “AS3 Redirect/Throw Events Forward and Into Other Files”