122 Chapter 15:Work with Objects and Classes Using ActionScript 2.0
About extending existing classes
The extends keyword in ActionScript 2.0 allows you to use all the methods and properties of an
existing class in a new class.For example,if you wanted to define a class that inherited everything
from the MovieClip class,you could use the following:
class Drag extends MovieClip
{}
The Drag class now inherits all properties and methods from the existing MovieClip class,and
you can use MovieClip properties and methods anywhere within the class definition,as in the
following example:
Note:The following ActionScript is an example only.You should not enter the script in your lesson
FLA file.
class Drag extends MovieClip
{
//constructor
function Drag ()
{
onPress=doDrag;
onRelease=doDrop;
}
private function doDrag():Void
{
this.startDrag();
}
private function doDrop():Void
{
this.stopDrag();
}
}
Note:The Convert to Symbol dialog box now offers a class field in which you can associate visual
objects (such as movie clip)with any class that you define in ActionScript 2.0.
Extend the MovieClip class to create a new class
You ’ll create a new class by extending the built-in MovieClip class.
1 Create a new Flash document and name it Shape.fla.
2 Using the drawing tools,draw a shape on the Stage.Right-click (Windows)or Control-click
(Macintosh)the shape and select Convert to Symbol from the context menu.
3 In the Convert to Symbol dialog box,select Movie Clip as the behavior,and click Advanced.
Select Export for ActionScript.
4 In the Name text box,enter myShape .
5 In the AS 2.0 Class text box,enter Drag .Click OK.
This associates the movie clip with the Drag class that you ’ll create.
6 Using the Property inspector,assign the movie clip an instance name.Then save the FLA file.
Note:An example finished file of the document you just created,named handson3.fla,is located in
your finished files folder.For the path,see “Set up your workspace ”on page 116.