Interfaces and Events with ActionScript and Flex - Handling Events
(Page 2 of 4 )
ActionScript 3.0 and the Flex framework use events to notify and receive notification when things occur. Events occur in response to the user (for example, the user clicks on something), time (timer events), and asynchronous messaging (such as remote procedure calls). Regardless of the cause of an event, nearly all ActionScript events use the same event model.
In MXML (Chapter 3), you saw how to use event handler attributes. In ActionScript, you can handle events by registering listeners. A listener is a function or method that should receive notifications when an event is dispatched. For example, you can register a method to receive a notification when the user clicks a button.
You need at least two elements to register a listener: an object that dispatches events, and a function that listens for events. Objects capable of dispatching events either extend theflash.events.EventDispatcherclass or implement theflash.events.IEventDispatcherinterface. When an object can dispatch events, it has a publicaddEventListener()method that requires at least two parameters; the name of the event for which you want to listen and the function/method that should listen for the event:
object.addEventListener("eventName", listenerFunction);
In most cases, the event names are stored in constants of the corresponding event type class. For example, the click event name is stored in theMouseEvent.CLICKconstant.
The listener function must expect one parameter of typemx.events.Eventor the relevant subclass ofEvent. For example, if the object dispatches an event of typeMouseEvent, the listener should accept aMouseEventparameter. The event parameter contains information about the event that occurred, including a reference to the object dispatching the event (thetargetproperty of the event object) and the object that most recently bubbled (relayed) the event (thecurrentTargetproperty). (In many cases, thetargetandcurrentTargetproperties reference the same object.) The following example adds an event listener using ActionScript, and when the user clicks the button, the listener displays the event object in an alert dialog box:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
initialize="initializeHandler(event)">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function initializeHandler(event:Event):void {
button.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void {
Alert.show(event.toString());
}
]]>
</mx:Script>
<mx:Button id="button" />
</mx:Application>
You can also unregister an event listener using theremoveEventListener()method. This method requires the same parameters asaddEventListener(). The method unregisters the specified listener function as a listener for the specified event. It is extremely important that you remove event listeners when they are no longer necessary. This includes all cases where you want to remove from memory the object listening for the events. Flash Player will not garbage-collect an object if there are any references to it still in memory. That means that even if an object is no longer used anywhere in the application, except for a reference held by an event dispatcher, it will not be garbage-collected.
The following example removes the event listener added in the previous example:
button.removeEventListener(MouseEvent.CLICK, onClick);
Error Handling
ActionScript 3.0 supports runtime error handling. That means that if and when an error occurs, the application can respond to the error in an elegant fashion rather than simply fail to work without any notification to the user. ActionScript 3.0 uses two types of runtime errors: synchronous and asynchronous.
Next: Handling Synchronous Errors >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|