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