ListenerProxy
ListenerProxy v2 – 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:
First of all, you need to add an instance of this class to stage. That should be the very first Line in your code. It can look like this:
-
package
-
{
-
import de.danielbunte.utils.listeners.ListenerProxy;
-
-
public class foo
-
{
-
public function foo():void
-
{
-
this.stage.addChild(ListenerProxy.getInstance());
-
}
-
}
-
}
That’s essential to get this thing work.
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 with type Object as parameter.
That’d look like this:
-
ListenerProxy.getInstance().addListener(new <sprite>[
-
mySprite_1,
-
mySprite_2,
-
mySprite_3
-
],
-
new <string>[
-
MouseEvent.MOUSE_OVER,
-
MouseEvent.MOUSE_OUT,
-
MouseEvent.CLICK
-
],
-
this.mouseHandler);
-
</string></sprite>
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
If you want to download and use this class, you have to agree to the following terms.
ListenerProxy:: L I C E N S E - I N F O R M A T I O N 1. Using this Library / Information about collection of usage-statistics This is an unregistered copy of ListenerProxy. You can use this software in non-commercial and commercial projects at no cost. Unregistered copies of this software always send a traceback to http://danielbunte.de. This traceback includes only the URL the software is used on and is used for collecting usage-statistics of this software. A traceback is not sent while developing on a local machine. Registered versions won't send a traceback, also the license-information can be deactivated. 1.1. Getting rid of the traceback You are not allowed to manipulate the software in any way to remove the traceback. If you'd like to get a traceback-free version of this software, just pass an e-mail to contact (at) badnoob (dot) com to register your copy. 2. Distribution of this software You are not allowed to distribute or sell this software without permission of the author. You are welcome to give the domain of this project http://danielbunte.de to other people by sending IMs, e-mails, chat, posting in forums, etc. 3. Decompilation of the SWC You are explicitly not allowed to decompile the SWC. If you need any further information on how the class works or if you need additional functionality, just pass an e-mail to: contact (at) badnoob (dot) com. 4. Pricing Unregistered copies are completely free of charge. Registered copies cost 10 Euros, once per domain. The pricing could change at any time. For pricing-information pass an e-mail to contact (at) badnoob (dot) com (Status quo 12th February 2010) 5. Warranty The author of the software does not warrant that it works as expected, neither does he warrant that there are no bugs in this software. The author of this software is not liable for any damages that were or seem to be produced by this software. You use this software at your own risk. 6. Agreement By downloading and/or using this software, you agree to the above terms. ListenerProxy:: End of L I C E N S E - I N F O R M A T I O N
I’ve read the above terms, agree to them and want to download the library now.