Creating a Class






Creating a Class

The code for defining an ActionScript class exists in its own .as file (remember that the .as file extension stands for Action Script). Only one class can be defined per file. If you create 13 classes, you have to create 13 .as files. A class file is nothing more than a text file containing ActionScript code. The name of each class file is the name of the class followed by the .as file extension. For example, if you create a TestClass class, it has to exist in a file with this exact name: TestClass.as.

Note

It's considered good coding practice to give classes a name with the first letter of each word capitalized. This notation is commonly referred to as camel-case.


A class file can be created from within the Macromedia Flash authoring environment by opening the ActionScript editor (File > New > ActionScript File) or by using the text editor of your choice. There are many third-party ActionScript editors that offer many of the same features as the built-in editor offered in Flash. In fact, most ActionScript programmers use programs like PrimalScript, SEPY, or Eclipse to write their ActionScript classes.

Simply creating a text file with an .as extension doesn't automatically make it a functional class file. The contents of the file must be ActionScript whose syntax defines the class (including its methods and properties). For example, the following code, inside the TestClass.as file, is the beginning of a valid class:

   class TestClass {
   }

The statement class tells the compiler (the part of Flash that handles the creation of SWF files) that everything that follows in this file is considered part of a class definition (for defining the TestClass class). After the class statement is the name of the class being created: TestClass. The name of the class must be the same as the name of the .as file that contains the code defining the class; therefore, the TestClass class definition must be in a file named TestClass.as.

A class file defines characteristics about the class by creating properties and methods. The properties and methods of a class are referred to as members, and they are all defined within the outermost curly braces of the class. Consider the following example:

    class Cube {
      var length:Number = 3;
      var height:Number = 2;
      var width:Number = 7;
      function getVolume():Number {
        return length * (height * width);
      }
     }

In this example, the properties length, height, and width and the method getVolume() are all members of the Cube class.

To create a new instance of a class to use its functionality in your project, the class must have a constructor method. However, you don't have to define the constructor in your class. If you leave the constructor out, a default constructor will be added automatically. The constructor must have the same name as the class. Let's modify the Cube class we just created to contain a constructor method and accept parameters so that we can create a custom cube instance:

   class Cube {
     var length:Number;
     var height:Number;
     var width:Number;
     function Cube(length:Number, height:Number, width:Number) {
       this.length = length;
       this.height = height;
       this.width = width;
     }
     function getVolume():Number {
       return length * (height * width);
     }
   }

Note

Because our constructor's parameters have the same name as the private properties in our class, we must use the keyword this to explicitly reference the class properties and differentiate between the constructor's parameters.


After coding the Cube class, you save it as an .as file named Cube.as in the same directory as your Flash authoring file. (Alternatively, you can place your .as class files elsewhere, which we'll discuss in a moment.) The name of the file must match the name of your class exactly. You would create a new instance of the Cube class on a frame in your Flash authoring file in the following way:

   var myCube:Cube = new Cube(3, 2, 7);

You can then get the volume of the myCube object this way:

   var volume:Number = myCube.getVolume();

By default, the properties and methods available to an instance of a class are fixed by the properties and methods defined in its class file. For example, the Cube class file defines three properties (length, height, and width) and one method (getVolume()). As a result, the myCube instance of the Cube class can access those particular properties and that method:

   myCube.length //has a value of 3
   myCube.height //has a value of 2
   myCube.width //has a value of 7
   myCube.getVolume() // returns a value of 42

The myCube properties can be set using the following syntax:

   myCube.length = 6;
   myCube.height = 4;

And so on. But if you attempt to add a property or method to the myCube instance that was not defined in its class file, such as the following, an error will result when you export your movie:

   myCube.color = "red";

This principle helps you eliminate bugs in your code. Here's how. In your application, perhaps Cube instances shouldn't have color properties, and instead you meant to assign a color property to the myCup instance of the Cup class, which would have been acceptable. When you export your movie, Flash's compiler sees that you set the color property for an instance of the Cube class. It then looks at the Cube class definition to see whether the color property has been defined there. If not, Flash assumes that the code is wrong, and an error results. This setup helps you to script object instances in a nearly foolproof manner; no object instance is allowed to perform an action unless that action is explicitly permitted by the class definition.

Although this can be a great way to help you debug your applicationsbecause Flash displays an error when you attempt to use an object in the wrong waythere might be times when you do want instances of a custom object class to be able to add or access properties and methods that were not defined in its class file. This is possible, too. You would simply need to add the dynamic class modifier to the class definition (notice the first line):

    dynamic class Cube {
      var length:Number;
      var height:Number;
      var width:Number;
      function Cube(length:Number, height:Number, width:Number) {
        this.length = length;
        this.height = height;
        this.width = width;
      }
      function getVolume():Number {
        return length * height * width;
      }
    }

With the addition of the dynamic modifier, all instances of the Cube class, including the myCube instance we created earlier, can add any property or method, regardless of whether that property or method was originally defined in the Cube class file.

Note

Some of Flash's built-in classesfor example, MovieClip, LoadVars, and SharedObjectare dynamic classes, which is why you can dynamically add properties and methods to instances of these classes. Other built-in classes such as TextField and Soundare not dynamic. If you attempt to add properties to instances of these classes, Flash displays errors when you export your movie.


There are a couple of important points to note about classes and class members.

Although you're not required to use strong typing syntax (:Number, :Array, :String, and so forth) when creating a class, this strategy is highly recommended. Strong typing is useful because it explicitly tells the compiler what you're trying to do, and the compiler can then help you to debug your application.

You can create instances of a custom class from any Timeline or class within your project, in the same manner as you do with any of Flash's built-in classes; therefore, an instance of our custom Cube class can be created on the main Timeline or on any movie clip's Timeline.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows