User Guide

86 Chapter 3: Using Best Practices
For more information on guidelines for writing ActionScript, see the following topics:
Adding initialization” on page 86
“Using trace statements” on page 87
“Using the super prefix” on page 87
Avoiding the with statement” on page 87
“Using variables” on page 88
“Writing syntax and statements” on page 89
“Following general formatting guidelines” on page 95
Adding initialization
One of the easiest ways to initialize code using ActionScript 2.0 is to use classes. You can
encapsulate all your initialization for an instance within the class’s constructor function, or
abstract it into a separate method, which you would explicitly call after the variable has been
created, as the following code shows:
class Product {
function Product() {
var prod_xml:XML = new XML();
prod_xml.ignoreWhite = true;
prod_xml.onLoad = function(success:Boolean) {
if (success) {
trace("loaded");
} else {
trace("error loading XML");
}
};
prod_xml.load("products.xml");
}
}
The following code could be the first function call in the application, and the only one you make
for initialization. Frame 1 of a FLA document that is loading XML might use code that is similar
to the following ActionScript:
if (init == undefined) {
var prod_xml:XML = new XML();
prod_xml.ignoreWhite = true;
prod_xml.onLoad = function(success:Boolean) {
if (success) {
trace("loaded");
} else {
trace("error loading XML");
}
};
prod_xml.load("products.xml");
init = true;
}