[Previous] [TOC]

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.

    With the entire shape selected, 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.

  7. A finished sample 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".

  8. Create an ActionScript file by selecting File > New > ActionScript File (Not Flash Document). Save the document with the name Drag.as, in the same location where you saved Shape.fla.

  9. 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;
      }
    }
    
  10. 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()
    }
    }
    
  11. Save the ActionScript file.

  12. Test the Shape.fla file.
    You should be able to drag the movie clip.

Congratulations on learning how to work with objects and classes in ActionScript 2.0. In a few minutes, you learned how to accomplish the following tasks:

  • Create and use objects from existing classes.

  • Create a custom class.

  • Create a property within a custom class.

  • Create a method within a custom class.

  • Extend an existing class and take advantage of inheritance.

[Previous] [TOC]