Creating the ActionScript class file

All component symbols are linked to an ActionScript 2.0 class file. (For information on linking, see Creating a component movie clip.)

To edit ActionScript class files, you can use Flash, any text editor, or any Integrated Development Environment (IDE).

The external ActionScript class extends another class (whether the class is a version 2 component, a version 2 base class, or the ActionScript MovieClip class). You should extend the class that creates the functionality that is most similar to the component you want to create. You can inherit from (extend) only one class. ActionScript 2.0 does not allow multiple inheritance.

This section contains the following topics:

Simple example of a component class file

The following is a simple example of a class file called MyComponent.as. If you were creating this component, you would link this file to the component movie clip in Flash.

This example contains a minimal set of imports, methods, and declarations for a component, MyComponent, that inherits from the UIComponent class. The MyComponents.as file is saved in the myPackage folder.

[Event("eventName")]

// Import packages.
import mx.core.UIObject;

// Declare the class and extend from the parent class.
class mypackage.MyComponent extends UIObject {

    // Identify the symbol name that this class is bound to.
    static var symbolName:String = "mypackage.MyComponent";

    // Identify the fully qualified package name of the symbol owner.
    static var symbolOwner:Object = Object(mypackage.MyComponent);

    // Provide the className variable.
    var className:String = "MyComponent";

    // Define an empty constructor.
    function MyComponent() {
    }

    // Call the parent's init() method.
    // Hide the bounding box--it's used
    // only during authoring.
    function init():Void {
        super.init();
    boundingBox_mc.width = 0;
    boundingBox_mc.height = 0;
    boundingBox_mc.visible = false;
} function createChildren():Void{ // Call createClassObject to create subobjects. size(); invalidate(); } function size(){ // Write code to handle sizing. super.size(); invalidate(); } function draw(){ // Write code to handle visual representation. super.draw(); } }

Overview of a component class file

The following is a general procedure that describes how to create an ActionScript file for a component class. Some steps may be optional, depending on the type of component you create.

To write a component class file:

  1. (Optional) Import classes. (See Importing classes).

    This allows you to refer to classes without writing out the package (for example, Button instead of mx.controls.Button).

  2. Define the class using the class keyword; use the extend keyword to extend a parent class. (See Defining the class and its superclass).
  3. Define the symbolName, symbolOwner, and className variables. (See Identifying the class, symbol, and owner names).

    These variables are necessary only in version 2 components.

  4. Define member variables. (See Defining variables).

    These can be used in getter/setter methods.

  5. Define a constructor function. (See About the constructor function).
  6. Define an init() method. (See Defining the init() method).

    This method is called when the class is created if the class extends UIComponent. If the class extends MovieClip, call this method from the constructor function.

  7. Define a createChildren() method. (See Defining the createChildren() method).

    This method is called when the class is created if the class extends UIComponent. If the class extends MovieClip, call this method from the constructor function.

  8. Define a size() method. (See Defining the size() method).

    This method is called when the component is resized, if the class extends UIComponent. In addition, this method is called when the component's live preview is resized during authoring.

  9. Define a draw() method. (See About invalidation).

    This method is called when the component is invalidated, if the class extends UIComponent.

  10. Add a Metadata tag and declaration. (See Adding component metadata).

    Adding the tag and declaration causes getter/setter properties to appear in the Property inspector and Component inspector in Flash.

  11. Define getter/setter methods. (See Using getter/setter methods to define parameters).
  12. (Optional) Create variables for every skin element/linkage used in the component. (See About assigning skins).

    This allows users to set a different skin element by changing a parameter in the component.


Flash CS3