O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Distinguishing Events Targeted at an Object from Events Targeted at That Object's Descendants

When the eventPhase variable of the Event object passed to a listener function is set to EventPhase.AT_TARGET, we know that the event dispatch is targeted at the object with which the listener registered. On the other hand, when eventPhase is set to either EventPhase.CAPTURING_PHASE or EventPhase.BUBBLING_PHASE, we know that the event dispatch is targeted at a descendant of the object with which the listener registered.

Therefore, a listener can use the following code to ignore events targeted at descendants of the object with which it registered:

private function someListener (e:SomeEvent):void {
  if (e.eventPhase == EventPhase.AT_TARGET) {
    // Code here is executed only when the object that registered this
    // listener is the event target.
  }
}

We can use the preceding technique to write code that responds to input received by a given object but not by any of its descendants. For example, imagine an application in which the Stage instance contains many buttons, text fields, and other input-receiving objects. To respond to mouse clicks when they occur over vacant areas of the Stage instance only, we use the following code:

// Register with the Stage instance for MouseEvent.CLICK events.
// As a result, clickListener() will be invoked when *any* object
// on the display list is clicked.
stage.addEventListener(MouseEvent.CLICK, clickListener);

// ...elsewhere, define the MouseEvent.CLICK event listener ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required