Events
EventDispatcher
is the base class for all objects that can be a target for
events. Many native classes inherit from this class with their own
appropriate events:
import flash.display.Sprite; import flash.events.MouseEvent; // this draws a button listening to a mouse event var sprite:Sprite = new Sprite(); sprite.addEventListener(MouseEvent.CLICK, onClick); import flash.utils.Timer; import flash.events.TimerEvent; // this creates a timer listening to a timer event var timer:Timer = new Timer(1000); timer.addEventListener(TimerEvent.TIMER , onTimer); timer.start();
event.target
is the object
associated with the listening function, or its scope:
function onClick(event:MouseEvent):void { trace(event.target); // Sprite } function onTimer(event:TimerEvent):void { trace(event.target); // Timer }
The listener object, also known as the event handler, is called
when the event happens (the onClick
or onTimer
functions in the preceding
example). It takes a single event instance as a parameter.
The dispatcher, the mechanism that monitors the event, saves a reference of the listener to call back when an event occurs, and its target, in a queue.
You must use removeEventListener
to remove these references
from memory. Removing the target will not do it:
sprite.removeEventListener(MouseEvent.CLICK, onClick); timer.removeEventListener(TimerEvent.TIMER , onTimer);
Event Propagation
For display objects, an event starts from the root parent, travels down the tree to the object that is registered, and ...
Get Developing Android Applications with Adobe AIR now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.