The
widgetCount
Widget class's constructor function is called, it adds 1 to
number of the current instance that's being created.
Save your file as Widget.as.
3.
Create a new FLA document, and save it as createWidget.fla in the same directory as Widget.as.
4.
In this file, you'll create new instances of the Widget class.
In createWidget.fla, select Layer 1 in the Timeline, and open the Actions panel (Window >
5.
Development Panels > Actions).
Add the following code to the Actions panel:
6.
// Before you create any instances of the class,
// widgetCount is zero (0)
trace("Widget count at start: " + Widget.widgetCount);
var widget_1:Widget = new Widget();
var widget_2:Widget = new Widget();
var widget_3:Widget = new Widget();
Save your work, and then select Control > Test Movie. You should see the following text in the
7.
Output panel:
Widget count at start: 0
Creating widget # 0
Creating widget # 1
Creating widget # 2
Class members and subclasses
Class members propagate to subclasses of the superclass that defines those members. In the
previous example (see
property to keep track of the number of instances of the class you created. You could create a
subclass of the Widget class, as shown in the following code:
class SubWidget extends Widget {
function SubWidget() {
trace("Creating subwidget # "+Widget.widgetCount);
}
}
The ActionScript 2.0 compiler can resolve static member references within class definitions. In
the previous example, if you don't specify the class name for the
but instead refer only to
is actually to
Widget.widgetCount
to the property as
not in your AS file) as
However, for optimal readability of your code, it is recommended that you always use explicit
references in your code, as shown in the previous example, to easily identify where the definition
of a static member resides.
266
Chapter 10: Creating Custom Classes with ActionScript 2.0
variable is declared as static, so it initializes to 0 only once. Each time the
"Using class members: a simple example" on page
, the ActionScript 2.0 compiler ascertains that the reference
widgetCount
and correctly exports that property. Similarly, if you referred
SubWidget.widgetCount
Widget.widgetCount
widgetCount
Widget.widgetCount
, the compiler rewrites the reference (in the bytecode,
because
is a subclass of the
SubWidget
and then shows the
265), you used a class
property,
class.
Widget
Need help?
Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?
Questions and answers