Specifying a root Timeline for loaded SWF files

The _root ActionScript property specifies or returns 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 specified by _root can change depending on whether a SWF file is running independently (in its own level) or has been loaded into a movie clip instance by a loadMovie() call.

For 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 movie clip target_mc.

// In container.swf:
_root.userName = "Tim";
target_mc.loadMovie("contents.swf");

The loaded SWF file, contents.swf, also declares a variable named userName on its root Timeline.

// In content.swf:
_root.userName = "Mary";

When 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". This could cause code in container.swf (as well as contents.swf) to malfunction.

To force _root to always evaluate to the Timeline of the loaded SWF file, rather than the actual root Timeline, use the _lockroot property. This property can be set either by the loading SWF file or the SWF file being loaded. When _lockroot is set to true on a movie clip instance, that movie clip will act as _root for any SWF file loaded into it. When _lockroot is set to true within a SWF file, that SWF file will act 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 attach the following code to the target_mc movie clip:

// Attached to target_mc movie clip:
onClipEvent (load) {
  this._lockroot = true;
}

This would ensure that references to _root in contents.swf—or any SWF file loaded into target_mc—will refer to its own Timeline, not the actual root Timeline of container.swf.

Equivalently, the author of contents.swf could add the following code to its main Timeline.

// Within contents.swf:
this._lockroot = true;

This would ensure that no matter where contents.swf is loaded, any reference it makes to _root will refer to its own main Timeline, not that of the hosting SWF file.

For more information, see MovieClip._lockroot.