AS3 Event Handling- part 5

Previous part of AS3 Event Handling series was about creating custom event class. In this part we will look at the one of the mostly unused parameters in addEventListener method handler. 

addEventListener is the method used to add an listener to an object for a specified event. Hence we usually pass two parameters ( first one is the event name and second event handler name) and everything works perfectly. But if you need more control over the event flow you may be interested in the other parameters available. The other three parameters are useCapture, priority & useWeakReference.   We will discuss about the first one in this part.

useCapture

This parameter tells about processing of event object in the phases of the event propagation. The default value is false, means event object can provide information about the target phase and the bubbling phase and not about the capture phase.

To understand the exact behaviour let’s create one small flash file and write some code. Open an AS3 flash document and create one big rectanglular movieclip and name is as “parent_mc” and a smaller rectangular movieclip inside it and name is as “child_mc“. Now select first frame and write the code. Download the file to see what the trace statements say about mouse down event.

  1. parent_mc.addEventListener(MouseEvent.MOUSE_DOWN, truePhase, true);
  2. parent_mc.addEventListener(MouseEvent.MOUSE_DOWN, falsePhase, false);
  3.  
  4. function truePhase(evt:MouseEvent):void{
  5.         trace("=====================use capture true===================================");
  6.         switch(evt.eventPhase){
  7.                 case EventPhase.BUBBLING_PHASE:
  8.                         trace("BUBBLING_PHASE");
  9.                         break;
  10.                 case EventPhase.CAPTURING_PHASE:
  11.                         evt.stopImmediatePropagation();
  12.                         trace("CAPTURING_PHASE");
  13.                         break;
  14.                 case EventPhase.AT_TARGET:
  15.                         trace("AT_TARGET");
  16.                         break;
  17.         }
  18.  
  19.         trace("currenttarget: " +evt.currentTarget.name);
  20.         trace("target:        "+evt.target.name);
  21. }
  22.  
  23. function falsePhase(evt:MouseEvent):void{
  24.         trace("=====================use capture false===================================");
  25.         switch(evt.eventPhase){
  26.                 case EventPhase.BUBBLING_PHASE:
  27.                         trace("BUBBLING_PHASE");
  28.                         break;
  29.                 case EventPhase.CAPTURING_PHASE:
  30.                         trace("CAPTURING_PHASE");
  31.                         break;
  32.                 case EventPhase.AT_TARGET:
  33.                         trace("AT_TARGET");
  34.                         break;
  35.         }
  36.  
  37.         trace("currenttarget: " +evt.currentTarget.name);
  38.         trace("target:        "+evt.target.name);
  39. }

There are two event handlers that handle the mouse down event, it’s because if we set useCapture true then we wont be able to catch the target & bubbling phase. So in two event handlers will handle the event with useCapture true and false respectively. When you down your mouse on the bigger rectangle, you can only see the target as parent_mc and in target phase of propagation. This is because there is no child element under the mouse pointer. Notice that I have added evt.stopImmediatePropagation(); in the first event handler in second switch statement, this blocks further propagation of event and do not let the second event handler get execute. The event object stops navigation. You can see the results when you click the inner child_mc in this example. You can only be able to see
=====================use capture true===================================
CAPTURING_PHASE
currenttarget: parent_mc
target: child_mc

If you uncomment that line then you will be able to see the events fired in bubbling phase also. So uncomment it and look at the results. You will see:
=====================use capture true===================================
CAPTURING_PHASE
currenttarget: parent_mc
target: child_mc
=====================use capture false===================================
BUBBLING_PHASE
currenttarget: parent_mc
target: child_mc

This tells us one good logic to control our events when we have to control the event flow while working with nested display objects.

Click here to download the example file.

This entry was posted in AS3, Flash. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>