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