![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
To create a class, you must first create an external ActionScript (AS) file. Classes (and interfaces) can only be defined in external script files. For example, you can't define a class in a script attached to a frame or button in a Flash document (FLA). To create an external AS file, use the ActionScript editor included with Flash or your preferred code or text editor.
Note: ActionScript code in external files is compiled into a SWF file when you publish, export, test, or debug a FLA file. Therefore, if you make any changes to an external file, you must save the file and recompile any FLA files that use it.
In the steps below you'll create a class called Person that contains two properties (age
and name
) and a single method (showInfo()
) that displays the values of those properties in the Output panel.
class Person { }
This is called the class declaration. In its most basic form, a class declaration consists of the class
keyword, followed by the class name (Person, in this case), and then left and right curly braces ({}
). Everything between the braces is called the class body and is where the class's properties and methods are defined.
Note: The name of the class (Person) matches the name of the AS file that contains it (Person.as). This is very important; if these two names don't match, the class won't compile.
var
keyword to define two variables named age
and name
, as shown below.
class Person {var age:Number;
var name:String;
}
Tip: By convention, class properties are defined at the top of the class body, which makes the code easier to understand, but this isn't required.
Notice the colon syntax (var age:Number
and var name:String
) used in the variable declarations. This is an example of strict data typing. When you type a variable in this way (var
variableName
:
variableType
), the ActionScript 2.0 compiler ensures that any values assigned to that variable match the specified type. Although this syntax is not required, it is good practice and can make debugging your scripts easier. (For more information, see Strict data typing.)
showInfo()
method, which returns a preformatted string containing the values of the age
and name
properties. Add the showInfo()
function definition to the class body, as shown below.
class Person {
var age:Number;
var name:String;
// Method to return property values
function showInfo():String {
return("Hello, my name is " + name + " and I'm " + age + " years old.");
}
}
Notice the use of data typing (optional but recommended) in the function definition.
function showInfo():String {...}
In this case, what's being typed is the showInfo()
function's return value (a string).
The constructor function always has the same name as the class. To create the class's constructor function, add the following code:
class Person {
var age:Number;
var name:String;
// Method to return property values
function showInfo():String {
return("Hello, my name is " + name + " and I'm " + age + " years old.");
}
// Constructor function
function Person (myName:String, myAge:Number) {
name = myName;
age = myAge;
}
}
The Person()
constructor function takes two parameters, myName
and myAge
, and assigns those parameters to the name
and age
properties. The two function parameters are strictly typed as String and Number, respectively. For more information about constructor functions, see Constructor functions.
Note: If you don't create a constructor function, an empty one is created automatically during compilation.
If you're using Flash MX 2004 (not Flash Professional), see Creating an instance of the Person class.
If any errors are reported in the Output panel, compare the code in your script to the final code in step 7, above. If you can't fix the code errors, copy the completed code in step 7.
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |