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

Creating Objects

To create an object from a class (known technically as instantiating the object), we use the keyword new in combination with the name of the class. The following generalized code shows the approach:

new ClassName

For example, to make an object from our VirtualPet class, we use the following code:

new VirtualPet

Multiple independent objects can be made from the same class. For example, the following code creates two VirtualPet objects:

new VirtualPet
new VirtualPet

Literal Syntax

We've just learned that the generalized syntax for creating a new object is:

new ClassName

That syntax applies to both built-in and custom classes. For example, the following code creates a new instance of the built-in Date class, which represents a particular point in time:

new Date

However, for some native classes, ActionScript also offers an alternative, more convenient means of creating instances, known as literal syntax. For example, to create a new Number instance representing the floating-point number 25.4, we can use the convenient literal form:

25.4

Likewise, to create a new String instance representing the text "hello," we can use the convenient literal form:

"hello"

Finally, to create a new Boolean instance representing the logical state of true, we can use the convenient literal form:

true

And to create a new Boolean instance representing the logical state of false, we can use the convenient literal form:

false

Literal syntax is also available for the Object, Function, RegExp, and XML classes. We'll study Object literal syntax in Chapter 15, Function literal syntax in Chapter 5, and XML literal syntax in Chapter 18. For information on RegExp literal syntax, see Adobe's documentation.

Object Creation Example: Adding a Pet to the Zoo

Now that we know how to create objects, we can add a VirtualPet object to our zoo program. The following code does just that:

package zoo {
  public class VirtualZoo {
    public function VirtualZoo () {
      new VirtualPet
    }
  }
}

Notice that the preceding code refers to the VirtualPet class by its unqualified name, VirtualPet—not by its qualified name, zoo.VirtualPet. Code in a given package can refer to the classes in that package by their unqualified names.

By contrast, code in a given package cannot refer to classes in other packages at all. To gain access to a public class in another package, we use the import directive, which has the following general form:

import packageName.className;

In the preceding code, packageName is the name of the class's package, and className is the name of the public class we wish to use. If the specified class is not public, the import attempt fails because a non-public class cannot be used outside its package. Once a class has been imported, it can then be referred to by its unqualified name. For example, to create an instance of the built-in flash.media.Sound class (which is used to load and play sounds), we would use the following code:

import flash.media.Sound
new Sound

Importing a class at the package-level makes that class available to code throughout the entire package body. For example, the following code imports flash.media.Sound at the package-level, and then later creates an instance of the Sound class within the VirtualZoo constructor method:

package zoo {
  import flash.media.Sound

  public class VirtualZoo {
    public function VirtualZoo () {
      new Sound
    }
  }
}

If a class's unqualified name conflicts with the unqualified name of another class, then qualified names must be used to differentiate the two classes. For example, if we were to define a class in the zoo package named Sound, then, within the zoo package, we would use the following code to create an instance of the built-in flash.media.Sound class (notice the use of the qualified name):

new flash.media.Sound

And we would use the following code to create an instance of the zoo package's Sound class:

new zoo.Sound

Use of the unqualified class name (e.g., Sound) on its own causes an error that prevents the offending program from compiling. Errors that prevent a program from compiling are known as compile-time errors.

To gain access to all the public classes in another package, we use the following code:

import packageName.*

For example, to gain access to all the public classes in the flash.media package, we use the following code:

import flash.media.*

Note that classes contained by a package that has no name are placed in an automatically created package known as the unnamed package. Classes in the unnamed package can be used directly anywhere in a program, without the need for the import directive. In other words:

package {
  // Classes defined here are in the unnamed package, and can be
  // used directly anywhere in a program
}

However, as a best practice, you should avoid defining classes in the unnamed package because their names might conflict with classes (and other kinds of definitions) defined by ActionScript, other programs, or even other parts of the same program.

Tip

On a technical level, the import directive opens the public namespace of the specified package for the current scope and all nested scopes. But if you are new to ActionScript, you needn't worry about the technical details of the import directive. We'll examine everything you need to know in the chapters to come.

Now let's return to the task of creating objects in the virtual zoo program. Recall the following code, which creates a new VirtualPet object (shown in bold):

package zoo {
  public class VirtualZoo {
    public function VirtualZoo () {
      new VirtualPet
    }
  }
}

The preceding code successfully creates a new VirtualPet object, but it also suffers from a problem: after the object has been created, the program has no way to refer to it. As a result, our program cannot subsequently use or control the new pet. To give our program a way to refer to the VirtualPet object, we use variables.

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