ListenerProxy


ListenerProxy v3.0.0 – Handle AS3 EventListeners easily

The ListenerProxy-Class is an easy to use controller for ActionScript 3 EventListeners. It helps you to easily keep track of all the Listeners you register onto an Object. You are able to
• Add/Remove single Listener to single Object
• Add/Remove single Listener to multiple Objects
• Add/Remove multiple Listeners to single Object
• Add/Remove multiple Listeners to multiple Objects
with a single line if you like :) I prefer using more than one line for better readability.
But there’s even more:
• Check if an object has a specified Listener
• Check if an object has a specified Listener, linked to a specified Function (remember, you’re able to register multiple Functions to one single Event in ActionScript by default)
• Print a list of all registered EventListeners of a specified Object

Here you get the usage-instructions:
Case 1 – You want to add one single EventListener to one single Object:

ListenerProxy.getInstance.addListener(mySprite, MouseEvent.MOUSE_OVER, this.mouseHandler);

This is the same as:

mySprite.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);

Case 2 – You want to add one single EventListener to multiple Objects:

ListenerProxy.getInstance.addListener([mySprite_1, mySprite_2, mySprite_3], MouseEvent.MOUSE_OVER, this.mouseHandler);

This is the same as:

mySprite_1.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);
mySprite_2.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);
mySprite_3.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);

Case 3 – You want to add multiple EventListeners to one single Object:

ListenerProxy.getInstance().addListener(mySprite, [MouseEvent.MOUSE_OVER, MouseEvent.MOUSE_OUT, MouseEvent.CLICK], this.mouseHandler);

N O T E: I always like to do some formatting like this:

ListenerProxy.getInstance().addListener(mySprite,
		                                [
		                                 MouseEvent.MOUSE_OVER,
		                                 MouseEvent.MOUSE_OUT,
		                                 MouseEvent.CLICK
		                                ],
		                                this.mouseHandler);

I like it that way and think it’s better readable, but just a tip.
The above Lines are the same like this:

mySprite.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);
mySprite.addEventListener(MouseEvent.MOUSE_OUT,  this.mouseHandler);
mySprite.addEventListener(MouseEvent.CLICK,      this.mouseHandler);

Case 4 – You want to add multiple EventListeners to multiple Objects:

ListenerProxy.getInstance.addListener([mySprite_1, mySprite_2, mySprite_3], [MouseEvent.MOUSE_OVER, MouseEvent.MOUSE_OUT, MouseEvent.CLICK], this.mouseHandler);

This is the same as:

mySprite_1.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);
mySprite_1.addEventListener(MouseEvent.MOUSE_OUT,  this.mouseHandler);
mySprite_1.addEventListener(MouseEvent.CLICK,      this.mouseHandler);
mySprite_2.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);
mySprite_2.addEventListener(MouseEvent.MOUSE_OUT,  this.mouseHandler);
mySprite_2.addEventListener(MouseEvent.CLICK,      this.mouseHandler);
mySprite_3.addEventListener(MouseEvent.MOUSE_OVER, this.mouseHandler);
mySprite_3.addEventListener(MouseEvent.MOUSE_OUT,  this.mouseHandler);
mySprite_3.addEventListener(MouseEvent.CLICK,      this.mouseHandler);

Instead of using an Array for multiple Listeners/Objects by using the braces [], you can also pass a Vector of type Object as parameter.
That’d look like this:

ListenerProxy.getInstance().addListener(new <object>[
                                        mySprite_1,
                                        mySprite_2,
                                        mySprite_3
                                       ],
                           new <string>[
                                        MouseEvent.MOUSE_OVER,
                                        MouseEvent.MOUSE_OUT,
                                        MouseEvent.CLICK
                                       ],
                           this.mouseHandler);</string></object>

(This damn codeparser has a major bug. Please ignore </string> and </object> tags.)
This is the recommended way – of course the array-version is easier to type and to teach.
For further information on Vector, see this page and especially the user-comments: http://livedocs.adobe.com/flex/3/langref/Vector.html

ListenerProxy v3.0.0 is now OpenSource! Find it at
http://wonderfl.net/c/5QOF

Comments (4)

  1. 06:41, 24. September 2012Chen  / Reply

    code does not compile as is. it doesn’t seem to recognize vector (line 326):
    [mxmlc] ListenerProxy.as:326): col: 73 Error: Syntax error: expecting rightbrace before semicolon.
    [mxmlc]
    [mxmlc] vecTypeIterator = objListenerType as Vector.;
    ^

    • 10:03, 24. September 2012Daniel Bunte  / Reply

      hi chen,
      in the error message you provided, there’s something missing. it should read “vecTypeIterator = objListenerType as Vector.<String>;” instead.
      did you use the code from wonderfl.net?

      regards,
      daniel

  2. 19:15, 24. September 2012Chen  / Reply

    Hello Daniel,

    line 326 where the first compilation error occurs reads:

    vecTypeIterator = objListenerType as Vector.;

    Yes, the code is from wonderfl.net

    Thanks

  3. 10:42, 25. September 2012Daniel Bunte  / Reply

    @Chen
    sry, this blog eats <> . please see my comment above, i updated it.
    the code on wonderfl is exactly what i’m using for production, so i really wonder why you get this error.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>