Creating a class file

To create a class, you must first create an external ActionScript (AS) file. Classes (and interfaces) can only be defined in external script files. For example, you can't define a class in a script attached to a frame or button in a Flash document (FLA). To create an external AS file, use the ActionScript editor included with Flash or your preferred code or text editor.

Note: ActionScript code in external files is compiled into a SWF file when you publish, export, test, or debug a FLA file. Therefore, if you make any changes to an external file, you must save the file and recompile any FLA files that use it.

In the steps below you'll create a class called Person that contains two properties (age and name) and a single method (showInfo()) that displays the values of those properties in the Output panel.

To create the class file:

  1. Create a new directory on your hard disk and name it PersonFiles. This directory will contain all the files for this project.
  2. Do one of the following:
    • Create a new file in your preferred text or code editor.
    • (Flash Professional only) Select File > New to open the New Document dialog box, select ActionScript File from the list of file types, and click OK. The Script window opens with a blank file.
  3. Save the file as Person.as in the PersonFiles directory.
  4. In the Script window, enter the following code:
    class Person {
    }
    

    This is called the class declaration. In its most basic form, a class declaration consists of the class keyword, followed by the class name (Person, in this case), and then left and right curly braces ({}). Everything between the braces is called the class body and is where the class's properties and methods are defined.

    Note: The name of the class (Person) matches the name of the AS file that contains it (Person.as). This is very important; if these two names don't match, the class won't compile.

  5. To create the properties for the Person class, use the var keyword to define two variables named age and name, as shown below.
    class Person {
    
      var age:Number;
      var name:String;
    
    }
    

    Tip: By convention, class properties are defined at the top of the class body, which makes the code easier to understand, but this isn't required.

    Notice the colon syntax (var age:Number and var name:String) used in the variable declarations. This is an example of strict data typing. When you type a variable in this way (var variableName:variableType), the ActionScript 2.0 compiler ensures that any values assigned to that variable match the specified type. Although this syntax is not required, it is good practice and can make debugging your scripts easier. (For more information, see Strict data typing.)

  6. Next you'll create the showInfo() method, which returns a preformatted string containing the values of the age and name properties. Add the showInfo() function definition to the class body, as shown below.
    class Person {
    
      var age:Number;
      var name:String;
    
      // Method to return property values
      function showInfo():String {
        return("Hello, my name is " + name + " and I'm " + age + " years old.");
      }
    }
    

    Notice the use of data typing (optional but recommended) in the function definition.

    function showInfo():String {...}
    

    In this case, what's being typed is the showInfo() function's return value (a string).

  7. The last bit of code you'll add in this section is a special function called a constructor function. In object-oriented programming, the constructor function initializes each new instance of a class.

    The constructor function always has the same name as the class. To create the class's constructor function, add the following code:

    class Person {
    
      var age:Number;
      var name:String;
      
      // Method to return property values
      function showInfo():String {
        return("Hello, my name is " + name + " and I'm " + age + " years old.");
      }
      
      // Constructor function
      function Person (myName:String, myAge:Number) {
        name = myName;
        age = myAge;
      }
    }
    

    The Person() constructor function takes two parameters, myName and myAge, and assigns those parameters to the name and age properties. The two function parameters are strictly typed as String and Number, respectively. For more information about constructor functions, see Constructor functions.

    Note: If you don't create a constructor function, an empty one is created automatically during compilation.

  8. Save the file as Person.as in the PersonFiles directory that you created in step 1.

    If you're using Flash MX 2004 (not Flash Professional), see Creating an instance of the Person class.

  9. (Flash Professional only) Check the syntax of the class file by selecting Tools > Check Syntax, or pressing Control+T (Windows) or Command+T (Macintosh).

    If any errors are reported in the Output panel, compare the code in your script to the final code in step 7, above. If you can't fix the code errors, copy the completed code in step 7.