O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

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


Like its name suggests, a package is a conceptual container for a group of classes and, as we'll learn later, for other things in a program. Each package delimits an independent physical region of a program and gives that region a name, called the package name. By convention, package names typically start with a lowercase letter while class names typically start with an uppercase letter. This helps distinguish package names from class names.

When a class's source code resides within a package, that class automatically adopts the package's name as part of its own name, much like a child takes on his parents' family name. For example, a class named Player in a package named game becomes known as game.Player. Notice that the package name comes first and is separated from the class name using a period (.) character (character is simply programming jargon for letter, number, punctuation, and so on). The package name helps distinguish the game.Player class from other classes also named Player, thus preventing name conflicts between different parts of a program or between a program's custom classes and ActionScript's built-in classes.

To create a new package, we use a package definition directive. Let's dissect that term. In ActionScript, all program instructions are known generally as directives. Definitions are one type of directive; they create, or define something, such as a package or a class. In this case, the thing being defined is a package, hence the term, package definition directive.


A definition that creates something in a program is said to define or declare that thing. Definitions are sometimes also referred to as declarations.

Here's the general form of a package definition directive:

package packageName {

All package definitions start with a keyword: package. A keyword is a command name reserved for use by the ActionScript language. In this case, the package keyword tells ActionScript to create a package. After the package keyword, we provide the desired package name, represented by packageName in the preceding code. (Throughout this book, italicized code, such as packageName, indicates text that must be replaced by the programmer.) Next, we mark the beginning and end of the package contents using curly braces: { and }. To add a class to a package, we insert its source code between the curly braces, as follows:

package packageName {
  Class source code goes here

In technical terms, the curly braces in a package definition are a kind of statement, known as a block statement. Like definitions, statements are a kind of directive, or basic program instruction. A block statement marks the beginning and end of a group of directives that should be treated as a logical whole. A package definition's block statement is known as a package block or sometimes package body.


For a complete list of ActionScript statements, see Chapter 10.

By convention (but not necessity), package names typically have the following structure:

  • The reversed domain name of the organization creating the program

  • Followed by a period (.)

  • Followed by the general purpose package's contents

For example, a package containing classes for a mapping application created by Acme Corp., whose domain name is acme.com, might be named com.acme.map, as shown in the following code:

package com.acme.map {

Notice that com precedes acme (i.e., in the package name, the domain name is reversed).


Domain names are guaranteed to be unique by the system of authorized top-level-domain registrars; thus, starting your package names with your organization's domain name avoids name conflicts with code developed by other organizations.

Now let's try using packages in our virtual zoo program. To keep our example simple, we'll use the package name zoo, without any leading domain name. To define the zoo package, we'll add the following code to the file VirtualZoo.as:

package zoo {

Now that we've added a package to the file VirtualZoo.as, we must change that file's location on the filesystem to match the package it contains. Due to a requirement imposed by all Adobe's ActionScript compilers, when a source file contains a class (or other definition) in a package, it must reside in a folder structure that matches that package name. For example, a file that contains a package named com.gamecompany.zoo must reside in a folder named zoo, in a folder named gamecompany, contained by a folder named com (i.e., com/gamecompany/zoo). Accordingly, we'll create a new folder named zoo in our program's file structure and move VirtualZoo.as into it. The file structure for our program's source files becomes:

   |- src
      |- zoo
         |- VirtualZoo.as

Now that we have a package definition, let's add the VirtualZoo class to it.

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