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 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.
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
.)
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...