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

Stopping an Event Dispatch

At any point during the event flow, every event listener—including those registered with the target object and those registered with its ancestors—can put a stop to the entire event dispatch. Stopping an event's dispatch is referred to as consuming the event.

To stop an event dispatch, we invoke the Event class's instance method stopImmediatePropagation( ) or stopPropagation( ) on the Event object passed to a listener function. The stopImmediatePropagation( ) method stops the event dispatch immediately, without allowing any remaining listeners to be triggered; the stopPropagation( ) method stops the event dispatch after ActionScript triggers the remaining listeners registered with object currently being notified of the event.

For example, suppose we have a Sprite, container, that contains a TextField, child:

var container:Sprite = new Sprite();
var child:TextField = new TextField();
child.text = "click here";
child.autoSize = TextFieldAutoSize.LEFT;
container.addChild(child);

Further suppose we have three event listener functions: containerClickListenerOne( ), containerClickListenerTwo( ), and childClickListener( ). Register containerClickListenerOne( ) and containerClickListenerTwo( ) with container for MouseEvent.CLICK event notification during the capture phase:

container.addEventListener(MouseEvent.CLICK,
                           containerClickListenerOne,
                           true);
container.addEventListener(MouseEvent.CLICK,
                           containerClickListenerTwo,
                           true);

Then we register childClickListener( ) ...

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