EasyManua.ls Logo

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

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...
Understanding classes and scope 285
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
static keyword specifies that a variable or function is created only once
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. For more information on static variables and functions, see
About public, private, and static methods and properties (members)” on page 247 and
“Using class members” on page 254.
Another benefit of static variables is that static variables dont lose their values after the
variables scope has ended. The following example demonstrates how you can use the
static
keyword to create a counter that tracks how many instances of the class Flash has created.
Because the
numInstances variable is static, the variable is created only once for the entire
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 (
User.numInstances) is incremented within the
constructor method.
3. Save the document as User.as.

Table of Contents

Related product manuals