Remember not to use too few or too many class files for your application, because doing so
can lead to poorly designed class files, which are not beneficial to the application's
performance or to your workflow. You should always attempt to use class files instead of
placing code in other places (such as timelines); however, avoid creating many classes that
have only a small amount of functionality or only a few classes that handle a lot of
functionality. Both of these scenarios might indicate poor design.
Using class members
One use of class (static) members is to maintain state information about a class and its
instances. For example, suppose you want to keep track of the number of instances that have
been created from a particular class. An easy way to do this is to use a class property that
increments each time a new instance is created.
In the following example, you'll create a class called Widget that defines a single, static
instance counter named
value of
widgetCount
the Output panel.
To create an instance counter using a class variable:
1.
Select File > New and then select ActionScript File, and then click OK.
2.
Type the following code into the Script window:
class Widget {
//Initialize the class variable
public static var widgetCount:Number = 0;
public function Widget() {
Widget.widgetCount++;
trace("Creating widget #" + Widget.widgetCount);
}
}
The
widgetCount
the Widget class's constructor statement is called, it adds 1 to
shows the number of the current instance that's being created.
3.
Save your file as Widget.as.
4.
Select File > New and then select Flash Document to create a new FLA, and save it as
widget_test.fla in the same directory as Widget.as.
254
Classes
. Each time a new instance of the class is created, the
widgetCount
increments by
and the current value of
1
variable is declared as static, so it initializes to 0 only once. Each time
is displayed in
widgetCount
and then
widgetCount
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?