Using methods and properties from a class file

In OOP, members (properties or methods) of a class can be instance members or class members. Instance members are created for each instance of the class; they are defined to the prototype of the class when they are initialized in the class definition. In contrast, class members are created once per class. (Class members are also known as static members.)

Properties are attributes that define an object. For example, length is a property of all arrays that specifies the number of elements in the array. Methods are functions that you associate with a class. For more information on functions and methods, see Functions and Methods.

The following example shows you how you would create a method in a class file:

class Sample {
  public function myMethod():Void {
    trace("myMethod");
  }
} 

Next you could invoke that method in your document. To invoke an instance method or access an instance property, you reference an instance of the class. In the following example, picture01, an instance of the custom Picture class (available in the following exercise), invokes the showInfo() method:

var img1:Picture = new Picture("http://www.helpexamples.com/flash/images/image1.jpg");
// Invoke the showInfo() method.
img1.showInfo();

The next example demonstrates how you can write a custom Picture class to hold various pieces of information about a photo.

To use the Picture and PictureClass classes in a FLA file:

  1. Select File > New and then select ActionScript File. Save the document as Picture.as and then click OK.

    You write your custom Picture class in this document.

  2. Type the following ActionScript code into the Script window:
    /**
        Picture class
        author: John Doe
        version: 0.53
        modified: 6/24/2005
        copyright: Adobe Systems Incorporated
    
        The Picture class is used as a container for an image and its URL.
    */
    
    class Picture {
        private var __infoObj:Object;
    
        public function Picture(src:String) {
            this.__infoObj = new Object();
            this.__infoObj.src = src;
        }
    
        public function showInfo():Void {
            trace(this.toString());
        }
        private function toString():String {
            return "[Picture src=" + this.__infoObj.src + "]";
        }
    
        public function get src():String {
            return this.__infoObj.src;
        }
        public function set src(value:String):Void {
            this.__infoObj.src = value;
        }    
    }
    
  3. Save the ActionScript file.
  4. Select File > New and then select Flash Document to create a new FLA file. Save it as picture_test.fla in the same directory as you saved the Picture class file.
  5. Type the following ActionScript code into Frame 1 of the Timeline:
    var picture1:Picture = new Picture("http://www.helpexamples.com/flash/images/image1.jpg");
    picture1.showInfo();
    this.createEmptyMovieClip("img_mc", 9);
    img_mc.loadMovie(picture1.src);
    
  6. Save the Flash document.
  7. Select Control > Test Movie to test the document.

    The following text is displayed in the Output panel:

    [Picture src=http://www.helpexamples.com/flash/images/image1.jpg]
    

For samples that demonstrates how to create a dynamic menu with XML data and a custom class file, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. The sample calls the ActionScript XmlMenu() constructor and passes it two parameters: the path to the XML menu file and a reference to the current timeline. Download and decompress the Samples zip file and navigate to the ActionScript2.0/XML_Menu folder to access these samples: