February 28, 2008
ActionScript 3.0: Events
Events are things that happen that Flash can recognize and respond to. A mouse click is an event, as are mouse movements and keypresses on the keyboard. Events can also be things that the user doesn't initiate. The completion of a sound, for example, is an event.
Anytime an event happens, an object of the Event class is created. When the mouse button is clicked, a MouseEvent object (a subclass of the Event class) is created. When a key on the keyboard is pressed, a KeyboardEvent object (another subclass of the Event class) is created. It may seem a little strange that an object represents an event, but remember Flash objects can be very abstract.
With all these events happening, you need a way to detect and respond to them. You detect an event by creating an event listener (or event handler).
An event listener is a simply a function that tells Flash what to do when a specific kind of event happens. Creating an event listener is a two-part operation: first you create a function, and second, you tell Flash where to put the listener.
For example, if you want to listen for a mouse click, you create a function with a parameter strictly typed to the MouseEvent object like so:
function reportClick (myevent:MoseEvent):void { // do something in response }
The word myevent is the parameter name that you make up that refers to the event. Next, you decide where you want to listen for the mouse click. If you want to listen for a mouse click on top of a particular button, then you add an event listener to that object:
myButton_btn.addEventListener(MouseEvent.CLICK, reportClick);
The addEventListener() method takes two parameters. The first is the specific kind of event that you want to detect. All the event objects have properties (like MouseClick.CLICK), which give more specificity to the event. The second parameter is the name of your function.
The actual object that receives the event can be referenced in the function by using the property target. In the example, the expression myevent.target references myButton_btn.
When you no longer need to listen for an event, you can delete the listener with the method removeEventListener(). The method takes two parameters, which are identical to the ones in the addEventListener() method.
Event Flow - Under the Hood
Event handling is a little more involved that what was just described above.
When an event occurs an Eventobject is created, the Event object systematically moves through other objects on the Flash Stage in a process known as the event flow. There are three parts to the event flow: a capture phase, a target phase, and a bubbling phase. Imagine that a mouse click happens on a button that is inside a movie clip on the Stage.
The MouseEvent object is created and is dispatched from the Stage and flows down to the movie clip and to the button inside the movie clip. That downward flow through those objects is the capture phase. The target phase involves the time the MouseEvent object is at the target (the button). Then it proceeds to bubble, or flow up the hierarchy, to the main Stage.
This round-trip flow is important because it lets you put a listener at any point along its path and still detect the event. In other words, the listener doesn't have to be tied to the object where the event occurs.
However, many events don't proceed through all three phases of the event flow. Some events, such as the Event.ENTER_FRAME object, are dispatched directly to the Stage and don't participate in a capture or bubbling phase.
This online article by Trevor McCauley on AS 3.0 event handling should give you a lot more information.
Video References (.zip)
