About writing subclasses in Flash
307
3.
Save your changes to the ActionScript document.
The Widget class now defines a constructor and a public method called
doSomething()
.
4.
Create a new ActionScript file named
SubWidget.as
and save it in the same directory
as Widget.as.
5.
In SubWidget.as, type the following ActionScript code into the Script window:
class SubWidget extends Widget {
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
doSomething();
}
}
6.
Save your changes to SubWidget.as.
Notice that the SubWidget class’s constructor calls the
doSomething()
method that you
defined in the superclass.
7.
Create a new Flash document and save it as
subWidgetTest.fla
in the same directory as the
ActionScript documents.
8.
In subWidgetTest.fla, type the following ActionScript into Frame 1 of the main Timeline:
var sw1:SubWidget = new SubWidget();
var sw2:SubWidget = new SubWidget();
9.
Save your changes to the Flash document.
10.
Select Control > Test Movie to test the Flash document. You see the following output in
the Output panel:
Creating subwidget # 1
Widget::doSomething()
Creating subwidget # 2
Widget::doSomething()
This output shows that the SubWidget class’s constructor calls the constructor of its
superclass (Widget), which increments the static
widgetCount
property. The SubWidget’s
constructor traces the superclass’s static property and calls the
doSomething()
method,
which inherits from the superclass.
NO
TE
If you created the SubWidget class in
“Example: Extending the Widget class”
on page 304
, you can use this file instead.
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...