Creating Your Own Custom Classes


Until this point, you've been saving your projects as .fla files and going back and forth between the timeline, the stage, the Actions panel, and other panels in the Flash authoring environment. Now you'll see a different kind of documentthe ActionScript (.as) file. An .as file is a text file that has a specific organized structure and holds only ActionScript code. It can be created either in the Flash authoring environment or in an external editing program.

Custom classes are saved in their own .as files. You can then use your custom classes just as you use the built-in classes. You can save custom classes in any location on your hard drive you wish, but for Flash to find these files when they're referenced from another file, you need to set the classpath. The classpath is a list of folders where Flash looks for external .as files.

Creating a New ActionScript File

To create a new .as file, go to File, New. This brings up the New Document dialog box. Select the General tab if it is not already selected to see a list of options. Select the ActionScript file option and click the OK button to create an .as file. You can now save this file to a location in your project directory for easy access.

Setting a Classpath

Flash doesn't automatically know where you keep your .as files. Because they are external files, much like external image files, you have to tell the compiler where to look for them. Use the following steps to set a classpath:

1.

Select File, Publish Settings to open the Publish Settings dialog box.

2.

Click the Flash tab (see Figure 13.1).

Figure 13.1. The Flash tab of the Publish Settings panel.


3.

Make sure that ActionScript 2.0 is selected in the ActionScript Version pop-up menu.

4.

Click on the Settings button. This brings up a panel for ActionScript 2.0 settings (see Figure 13.2).

Figure 13.2. The ActionScript Settings panel showing the Classpath list.


5.

Set the Export Frame for classes in this file. The default is the first frame.

6.

Select (or create) a folder on your system as a classpath.

To delete a folder from the classpath, select the path in the Classpath list and click the Remove from Path button.

Tip

As you work on more projects in Flash, you might develop an extensive library of custom classes and their .as files. You can keep them organized by project, by common classes, or any other way that works well for you. You might find it useful to keep them in a place that is easy to find on your system. I keep my .as files, organized by project, in a directory that is easy for me to find and frequently save a backup copy of it.


Class Structure

The ActionScript code in a class is organized into a specific structure. Each class file has a filename (which is the same as its class declaration), and consists of a class declaration, variable declarations, a class constructor, and class methods.

It's good practice to begin your class file with a comment block that gives the class name and purpose, author name and contact information, initial authoring time, and revision notes and dates, as shown in the following example:

/* Class Name Author Name/Contact Initial Author date/Revision date Notes: */


The class declaration is simply the class keyword followed by the class name:

class myClass{ . . . }


The first elements to add inside the class declaration are the variable declarations (note that new code has been shaded):

class myClass {     // declare class variables     var my_var1:Number;     var my_var2:Srting;     . . . }


Next, add the class constructor. This defines what properties an instance of your class will have when it's created. If the constructor is empty, no initial properties are defined for the class:

class myClass {     // declare class variables     var my_var1:Number;     var my_var2:String;     //class constructor     function myClass(){     } . . . }


Finally, write the methods for the class:

class myClass {     // declare class variables     var my_var1:Number;     var my_var2:Srting;     //class constructor     function myClass(){     }     //methods     function myMethod(){         //do something here     } }


Earlier in this chapter you saw the creation of an instance of the Object class named guest. We can also create your own custom class for that project.

First, declare the class. When you create a new class, it is usually a good idea to use comments to outline the code you'll be writing. That is what is also done here, as shown in the following example:

class Guest{     // declare variables     //class constructor     // define methods }


Earlier in this chapter, an instance of the Object class was created and given an instance name of guest. In the same statement, properties of that instance were simultaneously declared and assigned values. Now that you are creating a custom class, you must first declare the properties, separately from assigning values to them:

class Guest{     // declare variables     var name:String;     var address:Object;     var age:Number;     var favFood:String;     var likesMushrooms:Boolean;     //class constructor     // define methods }


Next, add the class constructor to the comment outline. The class constructor is similar to the functions you've seen in other chapters except that only properties are defined inside the curly braces. These properties are applied to each instance of your class at initialization (when the instance is created). When you create an instance of your class, you can pass values for its properties, as arguments, to the class constructor.

Take a look at the following codespecifically, the code below the //class constructor comment. First, the function keyword is used, followed by the name of the class, to signal to the compiler that this is the class constructor. Then, the parameters (property values) are listed between the parentheses. These are similar to the variables that you can pass to a function. Within the curly braces of the constructor you can assign the values of the variables you passed to the constructor as values for the properties of the class.

class Guest{     // declare variables     var name:String;     var address:Object;     var age:Number;     var favFood:String;     var likesMushrooms:Boolean;     // class constructor     function Guest(cNname, cAddress, cAge, cFavFood, cMush){         name = cName;         address = cAddress;         age = cAge;         favFood = cFavFood;         likesMushrooms = cMush;     }     // define methods }


Finally, define methods for the class. People can be fickle, and favorite foods can change. The getFavFood method can be used to return the favorite food of a guest by returning the current value of the instance's favFood property. The setFavFood method enables you to change the value of the guest's favFood property by passing a new value for it through the function argument (between the parentheses).

class Guest{     // declare variables     var name:String;     var address:Object;     var age:Number;     var fav_food:String;     var likesMushrooms:Boolean;     // class constructor     function Guest(g_name, g_address, g_age, g_fav_food, g_mush){         name = g_name;         address = g_address;         age = g_age;         fav_food = g_fav_food;         likesMushrooms = g_mush;     }     // define methods     function setFavFood(food){         favFood = food;     }     function getFavFood(){         return favFood;     } }


Creating an Instance from Your Class

After you create a custom class, you always use a class constructor statement to create an instance of the class. The syntax for creating an instance of a class is as follows:

// create a new Dinner instance var myDinner:Dinner = new Dinner();


You can use the class in any ActionScript, including an .fla file or another .as file. To make a custom class accessible from another file, you must use the import statement at the top of the code in that file. For example, to use the Guest class in an .fla file, type the following code into the first line of the Actions panel:

import Guest;


Defined classpaths tell the compiler where to look for the class, but if you create folders within that destination and place the class file inside a subdirectory, you must specify the name of that subdirectory in the import statement. For example, if you have a common classpath to all your .as files, and within that folder you have a folder for a project named DinnerParty, you would use the following import statement:

import DinnerParty.Guest;


Continuing with the custom Guest class just created, the following is an example of creating a Guest instance. To make the code easier to read, assign the values to local variables, and then pass these through to the class constructor.

// set up local vars to hold the values we'll pass to the class constructor var cName = "Jill Smith"; var cAddress = new Object;     cAddress.street = "123 anystreet";     cAddress.city = "anytown";     cAddress.state = "home state";     cAddress.zip = 22222; var cAge = 3; var cFaveFood = "mac and cheese"; var cMush = false; // create a new instance of the Guest object var guest1:Guest = new Guest(cName, cAddress, cAge, cFavFood, cMush);


When you have an instance, you can invoke a method from its class. For example, you can now reset a guest's favorite food to risotto. To use the class in another class, or an .fla, you must use the import command. If the Guest class was saved in a folder named dinnerParty, you'd use the following line to import it:

import dinnerParty.Guest


From anywhere in the file into which the class has been imported, including from an .fla, you can now use the following code:

//change guest's favorite food to risotto guest1.setFavFood("risotto");


Now, although you're not likely to edit code to change a dinner guest's favorite food, you could use a class like this along with database management classes as part of an event-planning application. This isn't a tutorial section, but I hope that this concrete example makes it easier for you to understand how to construct your own classes.



Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net