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:

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

This is the same as:

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

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

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

This is the same as:

ActionScript
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:

ActionScript
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:

ActionScript
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:

ActionScript
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:

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

This is the same as:

ActionScript
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:

ActionScript
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 (0)

› No comments yet.

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> <pre lang="" line="" escaped="" highlight="">