3.
Save the previous ActionScript code as Product.as.
4.
Create a new Flash document named testProduct.fla in the same directory.
5.
Select Frame 1 of the main Timeline.
6.
Type the following ActionScript into the Actions panel:
var myProduct:Product = new Product("http://www.helpexamples.com/
crossdomain.xml");
7.
Select Control > Test Movie to test this code in the test environment.
The contents of the specified XML document appear in the Output panel.
Another type of scope you encounter when working with these classes is static variables and
static functions. The
per class rather than being created in every instance of that class. You can access a static class
member without creating an instance of the class by using the syntax
someClassName.username
"About public, private, and static methods and properties (members)" on page 247
"Using class members" on page
Another benefit of static variables is that static variables don't lose their values after the
variable's scope has ended. The following example demonstrates how you can use the
keyword to create a counter that tracks how many instances of the class Flash has created.
Because the
numInstances
class, not for every single instance.
To use the static keyword:
1.
Select File > New and then select ActionScript File, and then click OK.
2.
Type the following code into the Script window:
class User {
private static var numInstances:Number = 0;
public function User() {
User.numInstances++;
}
public static function get instances():Number {
return User.numInstances;
}
}
The previous code defines a User class that tracks the number of times the constructor has
been called. A private, static variable (
constructor method.
3.
Save the document as User.as.
keyword specifies that a variable or function is created only once
static
. For more information on static variables and functions, see
254.
variable is static, the variable is created only once for the entire
User.numInstances
) is incremented within the
Understanding classes and scope
and
static
285
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?