About writing subclasses in Flash
305
5.
In SubWidget.as, type the following code into the Script window:
class SubWidget extends Widget {
public function SubWidget() {
trace("Creating subwidget #" + Widget.widgetCount);
}
}
6.
Save your changes to SubWidget.as.
7.
Create a new FLA file, and save it as
subWidgetTest.fla
in the same directory as the
previous ActionScript class files.
8.
In the subWidgetTest.fla file, type the following code into Frame 1 of the main Timeline:
var sw1:SubWidget = new SubWidget();
var sw2:SubWidget = new SubWidget();
trace("Widget.widgetCount = " + Widget.widgetCount);
trace("SubWidget.widgetCount = " + SubWidget.widgetCount);
The previous code creates two instances of the SubWidget class: sw1 and sw2. Each call to
the
SubWidget
constructor traces the current value of the static
Widget.widgetCount
property. Because the SubWidget class is a subclass of the Widget class, you can access the
widgetCount
property through the SubWidget class, and the compiler rewrites the
reference (in the bytecode, not in your ActionScript file) as
Widget.widgetCount
. If you
try to access the static
widgetCount
property off of instances of the Widget or SubWidget
class, like sw1 or sw2, the compiler throws an error.
9.
Save your changes to the document.
10.
Select Control > Test Movie to test the Flash document.
The Output panel displays the following output:
Creating subwidget #1
Creating subwidget #2
Widget.widgetCount = 2
SubWidget.widgetCount = 2
You see this output because even though the Widget class’s constructor is never explicitly
called, the SubWidget class’s constructor calls it for you. This causes the Widget class’s
constructor to increment the Widget class’s static
widgetCount
variable.
The ActionScript 2.0 compiler can resolve static member references within
class definitions.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...