AS3 Event handling- Part 3

Posted on March 30, 2009


In previous post we got familiarized with some key words that are commonly used. Now let’s grab an example and try to understand the basic concepts.

Create a movieclip instance on the stage and give it an instance name clip_mc . Now you can either write your code in an external file or in the frame. For more simplicity let’s write the code in the first frame. Here goes the code.

  1. clip_mc.addEventListener(MouseEvent.CLICK, clickEventHandler);
  2. function clickEventHandler(event:MouseEvent){
  3.      trace("Hello Mr. X, you have clicked me");
  4. }

Test the movie. Click on the clip_mc and you will find the string which we traced  in the output panel. This tells us that our program is correct. click_mc dispatches a click event when ever mouse is being clicked on it and our program is an listener to that event. Hence clickEventHandler is being called so that we can perform necesssary actions when this event occures.

Event flow:

The event flow describes how an event object moves through the display list. when we have more than one nested displayobjects inside a movieclip, event object makes a ‘U’ turn through the stage and the actual target object. So we have three stages(phases) of event flow. In the first stage it moves from the top-most parentobject(usually the stage) to the parent of the target object. The penitration phase is called Capture Phase, when event reaches the target it’s called Target Phase, and the returning phase is called Bubbling Phase. Here is the image that shows these phases ( I took this image from the help files of flash cs3).

 

This flow of event can be controlled by actionscript. You can stop the propagation at any point of time or not let the event reach the target phase also. Which makes it easier to get work done by the parent elements when some event on the inner most child node is fired. 

It’s note worthy to remember that there are exceptions to this event flowing. Some events like init & enterFrame do not have capture or bubbling phase. They have only target phase. So other objects are not affected by these events. Event’s that are not attached to any display objects are also in the stream of the above kind of events. For example netStatus , metadata event etc.


Posted Under AS3 »


Comments

Leave a Reply