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
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
value of the property.
6.
Select Control > Test Movie to test your document.
You will see
Global access
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
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
outside of the class definition.
9.
In the Actions panel, delete the
property is a static property, which means that the property applies
EXAMPLE_STATIC
in the Output panel.
and
EXAMPLE_PRIVATE
property is a public property, it is available to code
EXAMPLE_PUBLIC
trace
property as static, you use this code to access the
properties are not static properties. When
statements that you added in steps 5 and 7.
About constants and keywords
137
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?