Flash
  Home arrow Flash arrow Page 2 - Interfaces and Events with ActionScript an...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Interfaces and Events with ActionScript and Flex
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-05-22

    Table of Contents:
  • Interfaces and Events with ActionScript and Flex
  • Handling Events
  • Handling Synchronous Errors
  • Handling Asynchronous Errors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Flash Articles
    More By O'Reilly Media


     

    Buy this book now. 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.

    FLASH ARTICLES

    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT