Movie clips are at the very core of ActionScript. Without movie clips there would be no visual element to a Flash movie, so you can see why they are incredibly important.

Unlike many other object types, movie clips are never instantiated using a constructor function (the MovieClip class constructor is used only when creating subclasses, as in Recipe 12.12). Instead, movie clips are created in one of four ways:

  • Manually (at authoring time)

  • Attached from the Library at runtime, using attachMovie( )

  • Duplicated from another movie clip at runtime, using duplicateMovieClip( )

  • Created as a new, empty movie clip at runtime, using createEmptyMovieClip( )

Once a movie clip has been instantiated, it can be controlled in a variety of ways. The timelines of movie clips can be controlled using playback methods such as play( ), stop( ), and gotoAndPlay( ). Movie clips can be assigned actions that occur when certain events are triggered, and movie clip attributes can be controlled such that you can affect position, rotation, transparency, and so on.

To control a movie clip using ActionScript, you must know the target path to that instance. Each movie clip in a movie can be targeted in one of two ways: using a relative path or an absolute path. In relative paths there is a known relationship between the timeline from which the instructions are being issued and the movie clip to which they are being addressed. For example, a movie clip can address itself in a relative fashion using the keyword this:

// Instruct a movie clip to play from within its own timeline.  );

Generally, if the this keyword is omitted, it is assumed:

// Instruct a nested movie clip to set its x position to 50.
this.myNestedMovieClip._x = 50;

// Perform same task. In this example, this is assumed.
myNestedMovieClip._x = 50;

However, in some cases, omitting this can be problematic because there are global functions with the same names as movie clip methods. For example, if you use the command stop( ), Flash might think you are using the global stop( ) function rather than the MovieClip.stop( ) method. Therefore, as a best practice, you should always explicitly use this when referring to the current timeline rather than relying on Flash to assume it.

You can also target a nested movie clip's parent using the _parent keyword.

When you use _parent, you should always use an explicit reference to the starting point. In some cases Flash assumes this, but in other cases it does not, leading to unexpected results. To solve the problem, specify the movie clip you are starting from, as seen in the following examples:

// Set the rotation of a parent movie clip to 36.
this._parent._rotation = 36;

// Set the alpha of a parent clip's parent to 50.
this._parent._parent._alpha = 50;

Absolute paths do not require you to know the relationship of the timeline from which the instructions are being issued to the targeted movie clip. Rather, you need to know only the full path to the movie clip from the main, or root, movie clip. The main movie clip in a Flash movie is given a special name, _root. Therefore, absolute target paths begin with _root (or _leveln in some cases, though this is not recommended):

// Instruct myNestedMovieClip to play. This instruction can be issued from any
// timeline with success.  );

Using the global function loadMovieNum( ), it is possible to load .swf files into different levels within the same movie. In these cases, each movie can be referenced using absolute paths that begin with _level0, _level1, _level2, etc. However, the loadMovie( ) method is the preferred technique for loading content into your movie, and it does not use levels (see Recipe 15.1).

    Part I: Local Recipes
    Part II: Remote Recipes