356 Working with Movie Clips
Specifying a root timeline for loaded SWF files
The _root ActionScript property specifies or contains a reference to the root timeline of a
SWF file. If a SWF file has multiple levels, the root timeline is on the level that contains the
currently executing script. For example, if a script in level 1 evaluates
_root, _level1 is
returned. However, the timeline that
_root specifies can change, depending on whether a
SWF file is running independently (in its own level) or was loaded into a movie clip instance
by a
loadMovie() call.
In the following example, consider a file named container.swf that has a movie clip instance
named
target_mc on its main timeline. The container.swf file declares a variable named
userName on its main timeline; the same script then loads another file called contents.swf into
the
target_mc movie clip:
// In container.swf:
_root.userName = "Tim";
target_mc.loadMovie("contents.swf");
my_btn.onRelease = function():Void {
trace(_root.userName);
};
In the following example, the loaded SWF file, contents.swf, also declares a variable named
userName on its root timeline:
// In contents.swf:
_root.userName = "Mary";
After contents.swf loads into the movie clip in container.swf, the value of userName that’s
attached to the root timeline of the hosting SWF file (container.swf) would be set to
"Mary"
instead of "Tim". This could cause code in container.swf (as well as contents.swf ) to
malfunction.
To fo r ce
_root to always evaluate to the timeline of the loaded SWF file, rather than the
actual root timeline, use the
_lockroot property. You can set this property either by the
loading the SWF file or by the SWF file being loaded. When
_lockroot is set to true on a
movie clip instance, that movie clip acts as
_root for any SWF file loaded into it. When
_lockroot is set to true within a SWF file, that SWF file acts as its own root, no matter what
other SWF file loads it. Any movie clip, and any number of movie clips, can set
_lockroot to
true. By default, this property is false.
For example, the author of container.swf could put the following code on Frame 1 of the
main Timeline:
// Added to Frame 1 in container.swf:
target_mc._lockroot = true;