Writing custom class files

The following example examines the parts of a class file. You learn how to write a class, and how you can modify the class to extend the ways that you can use it with Flash. You learn about the parts of a class and how to import them as well as related information about working with custom class files in Flash.

You begin by looking at a very simple class. The following example shows the organization of a simple class called UserClass.

To define a class, you use the class keyword in an external script file (that is, not in a script you are writing in the Actions panel). The class structure is also pertinent for interface files. This structure is illustrated below, and following this illustration you create a class.

The following example looks at a simple ActionScript class named User.

To create class files:

  1. Select File > New and then select ActionScript File, and then click OK.
  2. Select File > Save As and name the new file User.as.
  3. Type the following ActionScript code into the Script window:
    /**
        User class
        author: John Doe
        version: 0.8
        modified: 08/21/2005
        copyright: Adobe Systems Incorporated
    
        This code defines a custom User class that allows you to create new users and specify user login information.
    */
    
    class User {
        // private instance variables
        private var __username:String;
        private var __password:String;
    
        // constructor statement
        public function User(p_username:String, p_password:String) {
            this.__username = p_username;
            this.__password = p_password;
        }
    
        public function get username():String {
            return this.__username;
        }
        public function set username(value:String):Void {
            this.__username = value;
        }
    
        public function get password():String {
            return this.__password;
        }
        public function set password(value:String):Void {
            this.__password = value;
        }
    }
    
  4. Save your changes to the class file.

    The previous code snippet begins with a standardized documentation comment, which specifies the class name, author, version, date the class was last modified, copyright information, and a brief description of what the class does.

    The User class's constructor statement takes two parameters: p_username and p_password, which are copied into the class's private instance variables __username and __password. The remainder of the code in the class defines the getter and setter properties for the private instance variables. If you want to create a read-only property, then you would define a getter function, but not a setter function. For example, if you want to make sure a user name cannot be changed after it has been defined, you would delete the username setter function in the User class file.

  5. Select File > New and then select Flash Document.
  6. Select File > Save As and name the file user_test.fla. Save the file in the same directory as User.as.
  7. Type the following ActionScript into Frame 1 of the Timeline:
    import User;
    var user1:User = new User("un1", "pw1");
    trace("Before:");
    trace("\t username = " + user1.username); // un1
    trace("\t password = " + user1.password); // pw1
    user1.username = "1nu";
    user1.password = "1wp";
    trace("After:");
    trace("\t username = " + user1.username); // 1nu
    trace("\t password = " + user1.password); // 1wp
    

    Because the User class you created previously is very basic, the ActionScript in the Flash document is also very straightforward. The first line of code imports the custom User class into your Flash document. Importing the User class lets you use the class as a custom data type.

    A single instance of the User class is defined and assigned to a variable named user1. You assign the user1 User object a value and define a username of un1 and a password of pw1. The following two trace statements display the current value of user1.username and user1.password using the User class's getter functions, which both return strings. The next two lines use the User class's setter functions to set new values for the username and password variables. Finally, you trace the values for username and password to the Output panel. The trace statements display the modified values that you set using the setter functions.

  8. Save the FLA file, and then select Control > Test Movie to test the files.

    You see the results of the trace statements in the Output panel. In the next examples, you use these files in an application.

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:


Flash CS3