I would have to say that getting my head around how listeners work in Actionscript might have been the toughest aspect of AS for me. But, once you get used to it, just like everything else, it’s simple to use.
To create a listener you need three things:
1-you create an object - new object().
2-you tell that object what to do when an event happens - event handler.
3-you get the object to listen for the event - addListener.
Here’s a real world analogy: suppose you want to make a phone ring when someone calls you.
First, (#1) create a phone.
var myPhone:Object = new Object();
Next, (#2) tell it what to do when a phone call comes in
myPhone.onCallRecieved = function():Void {
makeTheRingSound();
}
And finally, (#3) tell it to listen for an incoming phone calls.
phoneNetwork.addListener(myPhone);
This last part, (3) might be the most confusing aspect so I’ll go into it with a little more detail.
Think of it this way: there’s a phone network out there that everyone is connected to. If someone uses their phone to dial your phone number then your phone will ring. Obviously you have to have a phone, the object (#1) and your phone must know what to do when it receives a phone call (#2). But there’s a little more to it isn’t there. You can’t just buy a phone and turn it on. First, to get it to work you have to register it with the network and tell it what your phone number is. That way when someone dials your number the network can determine who is listening - your phone - and send you the message.
If you hadn’t done this last step then when someone dialed your number the network would still get the event but it wouldn’t know who to tell. Once you register with the network then it will know to broadcast an event to you when your number is dialed.
So once again, you have the object - phone - the thing in your phone that acts when it receives an onCallReceived event and finally you tell the network to let you know when someone dials your number.
General form of a listener:
Here is a very simple example of a listener in Actionscript. Use the arrow keys on your keyboard to move the ball up/down, left/right. The longer you hold down a key the faster/slower it will move. So holding down on the right-arrow for a few seconds will make it move right and then holding down the left-arrow for a few seconds will slow it back down. At any time you can hit the spacebar to stop the ball and return it to the center. (Note: you must click on the SWF - black square - to give it focus so that it receives the keyboard input.)
Here’s the code that makes this all work (download zip)
Here is what’s happening above. An object is created -
listener. And that object is told what to do if it gets an event called
onKeyDown. And finally we tell the
Key class to notify our
listener when the
onKeyDown event happens.
Suppose you had a Flash movie that doesn’t have any of this code. What happens when we press a key then? Well, the Flash player knows that a key was pressed and it - the Key class - looks to see if any objects need to be notified. If there are no listeners then obviously it doesn’t send any notifications.
The onKeyUp is the other event that the Key class can notify an object about. Since our listener is receiving all the events that pertain to the Key class then it gets those notifications as well. However, we don’t have any code that tells our listener what to do about it so nothing happens.
You might ask yourself, “why don’t mouse events work the same way?” That’s because the Flash developers thought that it would be so common to have mouse events associated with MovieClips and buttons that they made it simple to use. Basically, things like MovieClips automatically listen for mouse events so all you need to do is supply the code for what you want to happen, as such:
myClip.onRelease = function():Void {
// do things when someone clicks and
// releases the mouse on this MovieClip
}
That’s the #2 part of our example above. Steps #1 and #3 are done for us to make our life/coding simpler.
There are all sorts of other classes that broadcast events that you might want to listen for. If you’re using the movieClipLoader class you might want to know when something has completed loading, or with a video that is streaming into your movie you might want to know when it has completed playing. You can also use a listener to tell when the stage has changed size, or when a webcam has changed it’s status. The list goes on and on. In fact, that’s what you want to look for in the Flash help files - on. Pick a class in the Actionscript Classes folder and look for methods that begin with on. Those are the events that it broadcasts so that you can write code to manage what happens when the event is triggered.