Home arrow Flash arrow Page 2 - ActionScript Syntax for Flex Applications
FLASH

ActionScript Syntax for Flex Applications


In this second part of a five-part series that focuses on using ActionScript in Flex applications, you will learn ActionScript's syntax and how to use it with Flex. 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). 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: 4 stars4 stars4 stars4 stars4 stars / 8
May 08, 2008
TABLE OF CONTENTS:
  1. · ActionScript Syntax for Flex Applications
  2. · Declaring Classes
  3. · Variables and Properties
  4. · Variables and Properties continued

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
ActionScript Syntax for Flex Applications - Declaring Classes
(Page 2 of 4 )

Next, let’s look at the basic syntax and structure of a class. At a minimum, all ActionScript 3.0 classes consist of the following elements:

  1. Class package declaration
  2. Class declaration

Additionally, classes almost always also haveimportstatements.

Creating class files

Each class must be defined in its own file. (There are a few unique exceptions, but in most practical cases, a class must be defined in its own file.) The name of the file must be the same as the name of the class it contains, and the file must use the .as file extension. For instance, if you want to define an Example class, you must create a file named Example.as.

Package declarations

The syntax for all ActionScript 3.0 classes begins with a package declaration. As discussed earlier in this chapter, packages are used to organize classes. A package name in ActionScript corresponds to the directory structure within which the ActionScript file is stored. Each directory and subdirectory is delimited by a dot (.) in a package name. For example, if a class is stored in the example subdirectory of a com directory, the package name would becom.example. A class’s package declaration uses thepackagekeyword followed by the package name. Opening and closing curly braces, which contain anyimportstatements and class declarations, follow thepackagedeclaration. The followingpackagedeclaration says that the enclosed class exists within thecom.examplepackage. This also means that the file must exist within a com/example directory relative to one of the source path directories:

  package com.example {
   
// Import statements go here.
   
// Class declaration goes here.
  }

It’s considered a best practice to place all class files within packages with the possible exception of main class files when creating ActionScript 3.0-only (non-Flex) applications.

Import statements

As noted earlier, import statements should appear within thepackage declaration, but not within the class declaration. (Technically,importstatements can be placed anywhere, but by convention, they should be placed within thepackagedeclaration, but not in the class declaration.) You mustimportany and all classes you intend to use.

ActionScript 3.0 classes don’t automatically import classes. The following example imports theURLLoaderandURLRequestclasses from the Flash Player API:

  package com.example{
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    // Class declaration goes here.
 
}

Class declaration

All public ActionScript 3.0 classes placed within package declarations must be declared using thepublickeyword, followed by theclasskeyword and the name of the class. Opening and closing curly braces then follow, within which you place the class definition. Class names always start with initial capital letters by convention. The following example declares anExampleclass in thecom.examplepackage:

  package com.example {
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    public class Example {
     
// Class code goes here.
    }
  }


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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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