EasyManua.ls Logo

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

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...
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 classs constructor is never explicitly
called, the SubWidget classs constructor calls it for you. This causes the Widget classs
constructor to increment the Widget classs static
widgetCount variable.
The ActionScript 2.0 compiler can resolve static member references within
class definitions.

Table of Contents

Related product manuals