AS3: The “target” Property of an Event
Apr 12th, 2008 by Leo Geis
If you have an hour to chop down a tree, spend 30 minutes sharpening your axe. Knowing and employing the target Property of your Event is analogous to sharpening your OOP axe.
The target Property is used (among other implementations) to specify an Object within an Event Listener Function (click to enlarge):
I have used “beginDrag” and “endDrag” as my Function Identifiers, while the default ActionScript Method Identifiers startDrag and stopDrag remain intact.
In the Functions constructed for use in Listeners, an Argument is compulsory. In the example above I have used the Identifier evt (which I picked up from one of the texts I use to study ActionScript)-you can use any legal Identifier you wish, but it must be Typed correctly, as evt is to MouseEvent (that is the Event Type the Function will be passed when it is Called).
evt, the MouseEvent, has a target Property by virtue of its inheritance from the Event Class. The MouseEvent Class has a Property, a Constant, called MOUSE_DOWN, and a Property target which is explained in the ASLR as:
The InteractiveObject instance under the pointing device. The
targetis not always the object in the display list that registered the event listener. Use thecurrentTargetproperty to access the object in the display list that is currently processing the event.
Clarifying:
currentTarget : Object
[read-only] The object that is actively processing the Event object with an event listener.
Thus, we are using the target property of our Event to assign our startDrag() and stopDrag() Methods! Brilliant, isn’t it?! No OOP gymnastics are necessary…each Function is self contained…dare I say, Encapsulated…Polymorphage? Each Function will work on whatever Object has its addEventListener Method invoked, be it demoSprite1, demoSprite2, or any Instantiation of DisplayObject or its Subclasses. Isn’t this true?
I’m finding that spending 30 minutes sharpening my target Event Property (in other words, “knowing it cold”) is paying off handsomely because I don’t have to think twice while building my DisplayObject Event Listeners.
Good thing, because thinking is not my strong suit.
L



