286 Classes
4. Select File > New and then select Flash Document to create a new FLA file, and save the
FLA file in the same directory as User.as.
5. Type the following ActionScript code in Frame 1 of the Timeline:
trace(User.instances); // 0
var user1:User = new User();
trace(User.instances); // 1
var user2:User = new User();
trace(User.instances); // 2
The first line of code calls the static instances() getter method, which returns the value
of the private static
numInstances variable. The rest of the code creates new instances of
the User class and displays the current value returned by the
instances() getter method.
6. Select Control > Test Movie to test the documents.
For information on using the
this keyword in classes, see “About using the this keyword in
classes” on page 262.
About top-level and built-in classes
In addition to the ActionScript core language elements and constructs (for and while loops,
for example) and primitive data types (numbers, strings, and Booleans) described earlier in
this manual (see Chapter 4, “Data and Data Types,” on page 71 and Chapter 5, “Syntax and
Language Fundamentals,” on page 113), ActionScript also provides several built-in classes
(complex data types). These classes provide a variety of scripting features and functionality. You
have used top-level classes and other built-in classes that are part of the ActionScript language
in earlier chapters, and you will use them throughout the remaining chapters. There are many
classes that ship with Flash that you use to create interactivity and functionality in your SWF
files, and you can even build complex applications using them. For example, you can use the
Math class to perform equations in your applications. Or you might use the BitmapData class
to create pixels and scripted animations.
Top-level classes, listed in “Top-level classes” on page 288, are written into Flash Player. In the
Actions toolbox, these classes are located in the ActionScript 2.0 Classes directory. Some of
the top-level classes are based on the ECMAScript (ECMA-262) edition 3 language
specification and are called core ActionScript classes. Examples of core classes are the Array,
Boolean, Date, and Math classes. For more information on packages, see “Working with
packages” on page 230.