When we use addEventListener() to assign a method or function to handle an event, that method or function receives an event object as its only argument. The event object has a few properties that help us to get information about the event we're handling. The events, such as MouseEvent.CLICK, for example, are nearly self-explanitory. Try running this code snippet in your timeline:
ActionScript 3.0:
-
var box:Sprite = new Sprite();
-
box.name = "box";
-
box.graphics.beginFill(0);
-
box.graphics.drawRect(0,0,100,100);
-
box.x = box.y = 50;
-
addChild(box);
-
-
box.addEventListener(MouseEvent.CLICK,
-
onClicked,
-
false, 0, true);
-
-
function onClicked(evt:Event):void {
-
trace("type: " + evt.type);
-
trace("bubbles: " + evt.bubbles);
-
trace("eventPhase: " + evt.eventPhase);
-
trace("cancelable: " + evt.cancelable);
-
-
trace("target: " + evt.target + " " + evt.target.name);
-
trace("currentTarget: " + evt.currentTarget +
-
" " + evt.currentTarget.name);
-
}
After you've pasted this code in your timeline, run your movie and click on the black box. The onClicked function will give you information about the CLICK event in your output window:
type: click
bubbles: true
eventPhase: 2
cancelable: false
target: [object Sprite] box
currentTarget: [object Sprite] box
The type property is a string of the event type. In our example the type is "click"—the string representation of the MouseEvent.CLICK constant. The bubbles property is a boolean that is true when the event bubbles up through the display list and false when it doesn't. The eventPhase property contains an integer representing the phase the event is in at the time the proeprty is queried (1 for capture, 2 for target, 3 for bubble). The cancelable property is a boolean that is true if default behaviors associated with an event can be disabled.
The last two properties are the most commonly used properties of the Event object: target and currentTarget. The target property contains a reference to the display object reached during the "target" phase. For a CLICK event this will normally be whatever display object is directly under the mouse when the CLICK event is triggered. You can see this clearly in the prior "Event Phases in Action" post.
The currentTarget property contains a reference to the display object that is actively processing the event. It's easy to get the target and currentTarget properties mixed up. To make this clearer, let's edit our timeline code slightly to get a better look at the difference. Instead of adding your event to btn, add it to the stage:
Change your addEventListener() method accordingly:
stage.addEventListener(MouseEvent.CLICK,
onClicked,
false, 0, true);
Now run your movie, click the black box, and take a look at your output window:
type: click
bubbles: true
eventPhase: 3
cancelable: false
target: [object Sprite] box
currentTarget: [object Stage] null
Comparing this to our previous output we can see that the event is no longer getting handled in the "target" phase. Since there is no longer a CLICK event directly associated with btn the event isn't handled until the bubble phase when the CLICK event makes it's way back up to the stage. The currentTarget property reflects this - it shows us that the object processing the event is the stage. This differentiation is important and powerful.
Continue reading 'The Event Object'