Extend the MovieClip class to create a new class
You'll create a new class by extending the built-in MovieClip class.
- Create a new Flash document and name it Shape.fla.
- 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.
- In the Convert to Symbol dialog box, select Movie Clip as the behavior, and click Advanced. Select Export for ActionScript.
- In the Name text box, enter myShape.
- 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.
- 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.
- Create an ActionScript file by doing one of the following:
- If you're using Flash MX 2004 Professional, select File > New > ActionScript File (Not Flash Document). Save the document with the name Drag, in the same location where you saved Shape.fla.
- If you're using Flash MX 2004, open a text editor, such as Notepad. Save the file with the name Drag.as, in the same location where you saved Shape.fla.
- In the ActionScript file that you just created, create a new class and constructor called
Drag
:
class Drag extends MovieClip
{
function Drag ()
{
onPress=doDrag;
onRelease=doDrop;
}
}
- Define private methods in the class that use the existing movie clip methods,
startDrag()
and stopDrag()
:
class Drag extends MovieClip
{
function Drag()
{
onPress=doDrag;
onRelease=doDrop;
}
private function doDrag():Void
{
this.startDrag();
}
private function doDrop():Void
{
this.stopDrag()
}
}
- Save the ActionScript file.
- Test the Shape.fla document. You should be able to drag the movie clip.
Note: An example of the ActionScript file you just created, named Drag.as, is located in your finished files folder. For the path, see Set up your workspace.