Home arrow Flash arrow Page 2 - Interfaces and Events with ActionScript and Flex
FLASH

Interfaces and Events with ActionScript and Flex


In this fourth part of a five-part series on using ActionScript in Flex applications, you will learn how to define interfaces, and handle events and errors. It is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
May 22, 2008
TABLE OF CONTENTS:
  1. · Interfaces and Events with ActionScript and Flex
  2. · Handling Events
  3. · Handling Synchronous Errors
  4. · Handling Asynchronous Errors

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials