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

Accessing the Object That Registered the Listener

During every event dispatch, the Event object passed to every event listener defines a currentTarget variable that provides a reference to the object with which the event listener registered. The following general event-listener code demonstrates; it outputs the String value of the object with which someListener( ) registered:

public function someListener(e:SomeEvent):void {
  // Access the object with which this event listener registered
  trace(e.currentTarget);
}

For events targeted at nondisplay objects, the value of the Event class's instance variable currentTarget is always equal to target (because listeners always register with the event target). For example, returning once again to the FileLoader class from Example 12-1, if we check the value of both e.currentTarget and e.target within completeListener( ), we find that those two variables refer to the same object:

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

  public class FileLoader extends Sprite {
    public function FileLoader () {
      var urlLoader:URLLoader = new URLLoader();
      urlLoader.addEventListener(Event.COMPLETE, completeListener);
      urlLoader.load(new URLRequest("someFile.txt"));
    }

    private function completeListener (e:Event):void {
      trace(e.currentTarget == e.target);  // Displays: true
    }
  }
}

However, as we'll learn in Chapter 21, for events targeted at display objects in a display hierarchy, listeners can register both with the event target and with ...

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