O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Custom Class

Problem

You want to write a custom class.

Solution

Save a new file with the .as file extension and the filename matching the name of the class. Then add the class definition to the file with the following structure:

package package  {
    public class Class {

    }
}

Discussion

As noted earlier, the class is the basic building block of all ActionScript 3.0-based applications, so it’s essential that you master the basics of writing a class. For starters, all classes must be placed in .as files, which are plain text files saved with an .as file extension. There can be only one public class definition per .as file, and the name of the file must be the same as the name of the class. For example, if you name a class Example then the definition must be saved in a file called Example.as.

In ActionScript 3.0 all classes must be placed in packages. A package is a way of organizing classes into groups, and in ActionScript 3.0 a package is synonymous with a directory on the filesystem. Packages are relative to the classpath (which is discussed in detail in Recipe 2.2), but for this initial discussion the classpath is defined as a path relative to the project (the .fla file, in the case of Flash or the main class or MXML document in the case of Flex). Therefore, the top-level package is synonymous with the project’s root. The package declaration is always the first thing that appears in a class file; an example syntax follows:

package name {

}

When the class is defined as part of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required