To create an instance of the Person class in a Flash document:
In Flash, select File > New, select Flash Document from the list of document types, and
1.
click OK.
Save the file as createPerson.fla in the PersonFiles directory you created.
2.
By saving your FLA and AS files in the same directory, you can refer to the class using the fully
qualified class name in step 6.
Select Layer 1 in the Timeline, and open the Actions panel (Window > Development
3.
Panels > Actions).
In the Actions panel, enter the following code:
4.
// assumes FLA and AS class file are in same directory
var person_1:Person = new Person("Nate", 32);
var person_2:Person = new Person("Jane", 28);
trace(person_1.getInfo());
trace(person_2.getInfo());
This code creates two instances of the Person class,
method on each instance.
getInfo()
Save your work, and then select Control > Test Movie. You should see the following text in the
5.
Output panel:
Hello, my name is Nate and I'm 32 years old.
Hello, my name is Jane and I'm 28 years old.
You should now have a basic understanding of how to create and use classes in your Flash
documents. The rest of this chapter discusses classes and interfaces in more detail.
Creating and using classes
As discussed in
"Using classes: a simple example" on page
declaration and the body. The class declaration consists minimally of the
followed by an identifier for the class name, then left and right curly braces ({}). Everything inside
the braces is the class body, as shown in the following example:
class className {
// class body
}
You can define classes only in AS files. For example, you can't define a class in a frame script in a
FLA file.
Class names must be identifiers—that is, the first character must be a letter, underscore (
dollar sign (
), and each subsequent character must be a letter, number, underscore, or dollar sign.
$
The class name must exactly match the name of the AS file that contains it, including
capitalization. In the following example, if you create a class called Shape, the AS file that contains
the class definition must be named Shape.as:
// In file Shape.as
class Shape {
// Shape class body
}
254
Chapter 10: Creating Custom Classes with ActionScript 2.0
and
person_1
person_2
250, a class consists of two parts: the
class
, and then calls the
statement,
), or
_
Need help?
Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?