ActionScript 3.0 ListenerProxy goes public

Hello everybody!
Since February I wanted to put my great ListenerProxy-class onto the page, but I forgot it every day… So now I’m here and remembered that there was a Class to publish!

Hey Daniel, tell me what the heck is a ListenerProxy and why did ya write that piece of code man? Let’s go on…
Since ActionScript is enourmously memory-consuming, I often tried to get rid of this. Once I found out that you can’t remove any objects that are still having a Listener registered on them. So what do you have to do, to get an object out of the memory? Setting the value to null or probably 0 (ints and so on), doesn’t seem to do the trick. You also have to remove the EventListeners – and of course – ALL of them!
Some time ago in an old Flash CS3-Version, you could find a listeners-Array of every object when you debugged the movie, but you can’t access this array – no way!
Well that was really annoising me, so I decided to write a class that keeps track of all the listeners registered on an object.

So what else can we do with your ListenerProxy, Daniel?
You probably already had some code where you needed to add multiple Listeners with the same callback-function. You then had to write code like this:

  1. var myLoader:Loader = new Loader();
  2.     myLoader.addEventListener(Event.COMPLETE,                this.loadHandler);
  3.     myLoader.addEventListener(IOErrorEvent.IO_ERROR,        this.loadHandler);
  4.     myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,    this.loadHandler);
  5.     myLoader.addEventListener(ProgressEvent.PROGRESS,        this.loadHandler);

This are four lines of code for the listeners…. Wanna do it within a single line? Watch this:

  1. ListenerProxy.getInstance().addListener(myLoader, [Event.COMPLETE, IOErrorEvent.IO_ERROR, HTTPStatusEvent.HTTP_STATUS, ProgressEvent.PROGRESS], this.loadHandler);

You can of course add a single Listener for a object, just pass the type as second parameter without the brackets:

  1. ListenerProxy.getInstance().addListener(myLoader, Event.COMPLETE, this.loadHandler);

Ok that’s nice, isn’t it? So you have multiple objects to listen for the same Event calling the same callback-function? Hmmm….

  1. var myLoader_1:Loader = new Loader();
  2. var myLoader_2:Loader = new Loader();
  3. var myLoader_3:Loader = new Loader();
  4. var myLoader_4:Loader = new Loader();
  5.     myLoader_1.addEventListener(Event.COMPLETE,    this.loadHandler);
  6.     myLoader_2.addEventListener(Event.COMPLETE,    this.loadHandler);
  7.     myLoader_3.addEventListener(Event.COMPLETE,    this.loadHandler);
  8.     myLoader_4.addEventListener(Event.COMPLETE,    this.loadHandler);

Argh*!$# I hate such crappy code :)

  1. ListenerProxy.getInstance().addListener([myLoader_1, myLoader_2, myLoader_3, myLoader_4], Event.COMPLETE, this.loadHandler);

Yeeehaa! It makes the whole thing a bit easier, don’t you think so?

And that’s not all. If you have multiple objects with multiple listeners and the same callback-function, you can simply pass the elements as arrays as shown above.

So what are the key-functions to use?

  1. 1. ListenerProxy.getInstance():ListenerProxy //Singleton-Pattern is used
  2. 2. addListener(objToAddTo:*, strListenerType:*, funcCallback:Function):void //to add Listeners
  3. 3. removeListener(objToRemoveFrom:*, strListenerType:*, funcCallback:Function):void //to remove Listeners
  4. 4. hasListener(objToAsk:*, strListenerType:String, funcCallback:Function = null):Boolean //check whether the specified object has that listener or not
  5. 5. pop(objToPop:*, blnRemoveAllListeners:Boolean = false):* //remove the object from ListenerProxy's Dictionary (the 2nd param is VERY USEFUL!)

Get the Code here!

[UPDATE]

::06th August 2009 { fixed bug with adding/removing multiple listeners to multiple objects; also remove duplicate entries of “if(objToAdd.hasOwnProperty(‘addEventListener’))” }

[/UPDATE]

Leave a comment

Name: (required)

E-Mail: (required)

Website:

Comment: