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

Event Listener Priority

By default, when multiple event listeners are registered for a single event type with a given object, those listeners are triggered in the order in which they registered. For example, in the following code two event listeners—completeListenerA( ) and completeListenerB( )—register with urlLoader for the Event.COMPLETE event. When the Event.COMPLETE event occurs, completeListenerA( ) executes before completeListenerB( ) because completeListenerA( ) registered before completeListenerB( ).

package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;

  public class FileLoader extends Sprite {
    public function FileLoader () {
      var urlLoader:URLLoader = new URLLoader();
      // Registration order determines execution order
      urlLoader.addEventListener(Event.COMPLETE, completeListenerA);
      urlLoader.addEventListener(Event.COMPLETE, completeListenerB);
      urlLoader.load(new URLRequest("someFile.txt"));
    }

    private function completeListenerA (e:Event):void {
      trace("Listener A: Load complete");
    }

    private function completeListenerB (e:Event):void {
      trace("Listener B: Load complete");
    }
  }
}

To alter the default order in which event listeners are triggered, we can use the addEventListener( ) method's priority parameter, shown in the following generic code:

addEventListener(type, listener, useCapture,priority, useWeakReference)

The priority parameter is an integer indicating the order in which the event listener being registered should be triggered, relative to other listeners ...

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