Friday, August 19, 2011

_root and _level

Often we get confused between the usage of _root and _level. The two are however very different from each other._root is the main timeline of the currently active swf file/movie clip whereas the _level defines the active order of the swf file. A swf file which loads a flash application is automatically treated as _level0. _root now refers to the main timeline of the _level0. Suppose the application loads another swf file, this would be addressed as _level1. When _level1 is in use, the _root will change from main timeline of _level0 to the main timeline of _level1.

Generalizing:  If a movie clip that contains _root is loaded into another movie clip, _root refers to the main timeline of the loading movie clip, and not the timeline that contains _root.

Any swf file can be loaded at any level. See an example below:

loadMovieNum("test.swf", 5);

The above code loads the "test" swf file at level 5. If we wish to

"_level" is accessed like any other movie. If we wish to stop the playhead of the main timeline of the swf file, we can add the following code:

_level5.stop()

 If we wish to execute data inside any particular frame (say frame 7), we can use the following chunk of code.

_level5.gotoAndStop(7)
 
 _root can be used similarly as follows: 
 
_root.stop() stops the Timeline of the level containing the currently executing script 
 
  
There is something what we call as _lockroot. _lockroot is used whenever we want to ensure that _root refers to the main timeline of the loaded movieclip even if another movieclip is also loaded. Setting this property to "true" does the required job.

myMovieClip._lockroot = true;

Where myMovieClip is the one to which _root should refer even if any other movieclip is loaded.

The concept of both _root and _level is absent in action script 3.0.