748 Best Practices and Coding Conventions for ActionScript 2.0
Avoiding absolute targets (_root)
You can use several methods to target instances that let you avoid using _root; these methods
are discussed later in this section. Avoid using
_root in ActionScript 2.0 because SWF files
that load into other SWF files might not work correctly. The
_root identifier targets the base
SWF file that is loading, not the SWF file using relative addressing instead of
_root. This
issue limits code portability in SWF files that are loaded into another file, and, particularly, in
components and movie clips. You can help resolve problems by using
_lockroot, but only
use
_lockroot when necessary (such as when you are loading a SWF file but do not have
access to the FLA file). For more information on using
_lockroot, see “Using _lockroot”
on page 748.
Use
this, this._parent, or _parent keywords rather than _root, depending on where your
ActionScript 2.0 code is located. The following example shows relative addressing:
myClip.onRelease = function() {
trace(this._parent.myButton._x);
};
All variables must be scoped, except for variables that are function parameters, and local
variables. Scope variables relative to their current path whenever possible, using relative
addressing, such as the
this property. For more information on using the this property, see
this property in the ActionScript 2.0 Language Reference.
Using _lockroot
You can use _lockroot to target content as a way to solve the scoping issues sometimes
associated with the inappropriate use of
_root. Although this solves many problems with
applications, consider
_lockroot as a work-around for problems caused by using _root. If
you experience problems loading content into a SWF file or a component instance, try
applying
_lockroot to a movie clip that loads the content. For example, if you have a movie
clip called
myClip loading content, and it stops working after it is loaded, try using the
following code, which is placed on a timeline:
this._lockroot = true;