EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 158

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
158 Chapter 9: Creating Classes with ActionScript 2.0
To create the class file:
1 Create a new directory on your hard disk and name it PersonFiles. This directory will contain
all the files for this project.
2 Do one of the following:
Create a new file in your preferred text or code editor.
(Flash Professional only) Select File > New to open the New Document dialog box, select
ActionScript File from the list of file types, and click OK. The Script window opens with a
blank file.
3 Save the file as Person.as in the PersonFiles directory.
4 In the Script window, enter the following code:
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 classs
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.
5 To create the properties for the Person class, use the 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” on page 38.)

Table of Contents

Related product manuals