250 Classes
For example, the top-level Math class consists only of static methods and properties. To call
any of its methods, you don’t create an instance of the Math class. Instead, you simply call the
methods on the Math class itself. The following code calls the
sqrt() method of the
Math class:
var squareRoot:Number = Math.sqrt(4);
trace(squareRoot); // 2
The following code invokes the max() method of the Math class, which determines the larger
of two numbers:
var largerNumber:Number = Math.max(10, 20);
trace(largerNumber); // 20
For more information on creating class members, see “About class members” on page 250 and
“Using class members” on page 254.
A sample file on your hard disk demonstrates how to create a dynamic menu with XML data
and a custom class file. The sample calls the ActionScript
XmlMenu() constructor and passes it
two parameters: the path to the XML menu file and a reference to the current timeline. The
rest of the functionality resides in a custom class file, XmlMenu.as.
You can find the sample source file, xmlmenu.fla, in the Samples folder on your hard disk.
■ On Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\XML_Menu.
■ On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/XML_Menu.
About class members
Most of the members (methods and properties) discussed so far in this chapter are of a type
called instance members. For each instance member, there’s a unique copy of that member in
every instance of the class. For example, the
email member variable of the Sample class has an
instance member, because each person has a different e-mail address.
Another type of member is a class member. There is only one copy of a class member, and you
use it for the entire class. Any variable declared within a class, but outside a function, is a
property of the class. In the following example, the Person class has two properties,
age and
username, of type Number and String, respectively:
class Person {
public var age:Number;
public var username:String;
}