EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 137

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 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...
About constants and keywords 137
For example, the class definition in the next example creates three constants that follow the
naming convention used by ActionScript 2.0.
To use constants in an application:
1. Select File > New and then select ActionScript File to create an AS file.
2. Name the new file ConstExample.as.
3. Type the following code into the Script window:
class ConstExample {
public static var EXAMPLE_STATIC:String = "Global access";
public var EXAMPLE_PUBLIC:String = "Public access";
private var EXAMPLE_PRIVATE:String = "Class access";
}
The EXAMPLE_STATIC property is a static property, which means that the property applies
to the class as a whole instead of to a particular instance of the class. You must access a
static property of a class using the name of the class instead of the name of an instance.
You cannot access a static property through a class instance.
4. Create a new Flash document and save it as const.fla.
5. Open the Actions panel, and type the following code on Frame 1 of the Timeline:
trace(ConstExample.EXAMPLE_STATIC); // output: Global access
When you declare the EXAMPLE_STATIC property as static, you use this code to access the
value of the property.
6. Select Control > Test Movie to test your document.
You will see
Global access in the Output panel.
7. In the Actions panel, type this code following the code you added in step 5.
trace(ConstExample.EXAMPLE_PUBLIC); // error
trace(ConstExample.EXAMPLE_PRIVATE); // error
8.
Select Control > Test Movie to test your document.
The
EXAMPLE_PUBLIC and EXAMPLE_PRIVATE properties are not static properties. When
you try to access the values through the class, you see the error message:
The property being referenced does not have the static attribute.
To access a property that is not static, you must access the value through an instance of the
class. Because the
EXAMPLE_PUBLIC property is a public property, it is available to code
outside of the class definition.
9. In the Actions panel, delete the trace statements that you added in steps 5 and 7.

Table of Contents

Related product manuals