Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Classes > Example: Writing custom classes | |||
Now that you've explored the basics of a class file, and what kinds of things it contains, it's time to learn some of the general guidelines for creating a class file. The first example in this chapter shows you how to write classes and package them. The second example shows you how to use those class files with a FLA file.
|
CAUTION |
|
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. |
As discussed in Writing custom class files, a class consists of two main parts: the declaration and the body. The class declaration consists minimally of the class statement, followed by an identifier for the class name, and then left and right curly braces ({}). Everything inside the braces is the class body, as shown in the following example:
class className {
// class body
}
Remember: you can define classes only in external ActionScript files. For example, you can't define a class in a frame script in a FLA file. Therefore, you create a new file for this example.
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.
By the end of this example, the basic ordering of your class files is as follows:
You do not write subclasses in this chapter. For more information on inheritance and subclassing, see Inheritance.
This example includes the following topics:
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:
The following points are guidelines to follow when you write custom class files. They help you write correct and well-formed classes. You practice these guidelines in upcoming examples.
private var SKU:Number; // product SKU (identifying) number private var quantity:Number; // quantity of product
i) before using it in the for loop:
var my_array:Array = new Array("one", "two", "three");
var i:Number;
for (i = 0 ; i < my_array.length; i++) {
trace(i + " = " + my_array[i]);
}
// bad code
var counter:Number = 0;
function myMethod() {
var counter:Number;
for (counter = 0; counter <= 4; counter++) {
// statements;
}
}
This code declares the same variable inside an inner block.
// bad form xPos = yPos = 15;
or
// bad form
class User {
private var m_username:String, m_password:String;
}
Class names must be identifiers--that is, the first character must be a letter, underscore (_), or dollar sign ($), and each subsequent character must be a letter, number, underscore, or dollar sign. As a preferred practice, try to always limit class names to letters.
The class name must exactly match the name of the ActionScript file that contains it, including capitalization. In the following example, if you create a class called Rock, the ActionScript file that contains the class definition must be named Rock.as:
// In file Rock.as
class Rock {
// Rock class body
}
You name and create a class definition in the following section. See the section Creating and packaging your class files to create, name, and package the class files. For more information on naming class files, see Naming classes and objects.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000789.html