Creating instances of classes in an example

Instances are objects that contain all the properties and methods of a particular class. For example, arrays are instances of the Array class, so you can use any of the methods or properties of the Array class with any array instance. Or you can create you own class, such as UserSettings, and then create an instance of the UserSettings class.

Continuing the example you started in Example: Using custom class files in Flash, you modified FLA file to import the classes you wrote so that you don't have to always refer to them by their fully qualified names.

The next step in this example (Example: Using custom class files in Flash) is to create an instance of the ClassA and ClassB classes in a script, such as a frame script in a package_test.fla Flash document, and assign it to a variable. To create an instance of a custom class, you use the new operator in the same way you would when creating an instance of a top-level ActionScript class (such as the Date or Array class). You refer to the class using its fully qualified class name, or import the class (as demonstrated in Importing classes and packages.)

NOTE

The following exercise is part of Example: Using custom class files in Flash which continues the examples Example: Writing custom classes.

To create a new instance of the ClassA and ClassB classes:

  1. Open the file called package_test.fla.
  2. Type the following boldface code into the Script window:
    import com.adobe.utils.*;
    var a:ClassA = new ClassA(); // ClassA constructor
    a.doSomething(); // call the ClassA's doSomething() method
    var b:ClassB = new ClassB(); // ClassB constructor
    b.doSomething(); // call the ClassB's doSomething() method
    

    Data typing your objects in this code example enables the compiler to ensure that you don't try to access properties or methods that aren't defined in your custom class. For more information on strict data typing, see About assigning data types and strict data typing. The exception to data typing your objects is if you declare the class to be dynamic using the dynamic keyword. See Creating dynamic classes.

  3. Save your changes to the FLA file before you proceed.

You should now have a basic understanding of how to create and use classes in your Flash documents. Remember that you can also create instances of top-level ActionScript or built-in classes (see About working with built-in classes).

To continue using these class files in a Flash file, see Assigning a class to symbols in Flash.