Organizing Movie Clips in Flash MX - Importing External Movies and Images
(Page 2 of 4 )
We’ve discussed creating movie clip instances within a single document, but the Flash Player can also display multiple .swf documents simultaneously. We can use loadMovie()—as either a global function or a movie clip method—to import an external .swf file into the Player and place it either in a clip instance or on a numbered level above the base movie (i.e., in the foreground relative to the base movie).
In Flash Player 6, loadMovie() can also load JPEG image files into a movie clip or document level. For details, see loadMovie() and MovieClip.loadMovie() in the Language Reference.
Dividing content into separate files gives us precise control over the downloading process and makes partial application updates easier. Suppose, for example, we have a movie containing a main navigation menu and five subsections. Before the user can navigate to section five, sections one through four must finish downloading. But if we place each section in a separate .swf file, the sections can be loaded in an arbitrary order, giving the user direct access to each section. To update a section, we can simply replace the appropriate .swf file with a new one.
When an external .swf is loaded into a level, its main movie timeline becomes the root timeline of that level, and it replaces any prior movie loaded in that level. Similarly, when an external movie is loaded into a clip, the main timeline of the loaded movie replaces that clip’s timeline, unloading the existing graphics, sounds, and scripts in that clip.
Like duplicateMovieClip(), loadMovie() can be used as both a standalone function and an instance method. The standalone syntax of loadMovie() is as follows:
loadMovie(url, location)
where url specifies the address of the external .swf file to load. The location parameter is a string indicating the path to an existing clip or a document level that should host the new .swf file (i.e., where the loaded movie should be placed). For example:
loadMovie("circle.swf", "_level1");
loadMovie("photos.swf", "viewClip_mc");
Because a movie clip reference is converted to a path when used as a string,location can also be supplied as a movie clip reference, such as_level1instead of"_level1". Take care when using references, however. If the reference supplied does not point to a valid clip, the loadMovie() function has an unexpected behavior—it loads the external .swf into the current timeline. See “Method Versus Global Function Overlap Issues ” later in this chapter for more information on this topic.
The MovieClip method version of loadMovie() has the following syntax:
theClip.loadMovie(url);
When used as a clip method, loadMovie() assumes we’re loading the external .swf intotheClip, so thelocation parameter required by the standalone loadMovie() function is not needed. Therefore, we supply only the path to the .swf to load via theurl parameter. Naturally,url can be either an absolute or a relative filename, such as:
viewClip.loadMovie("photos.swf");
When placed into a clip instance, a loaded movie adopts the properties of that clip (e.g., the clip’s scale, rotation, color transformation, etc.).
Note thattheClip must exist in order for loadMovie() to be used in its method form. For example, the following attempt to load circle.swf will fail if_level1is empty:
_level1.loadMovie("circle.swf");
Using loadMovie() with attachMovie()
Loading an external .swf file into a clip instance with loadMovie( ) has a surprising result—it prevents us from attaching instances to that clip via attachMovie(). Once a clip has an external .swf file loaded into it, that clip can no longer bear attached movies from the Library from which it originated. For example, if movie1.swf contains an instance named clipA, and we load movie2.swf intoclipA, we can no longer attach instances toclipAfrom movie1.swf’s Library.
Why? The attachMovie( ) method works only within a single document. That is, we can’t attach instances from one document’s Library to another document. When we load a .swf file into a clip, we are populating that clip with a new document and, hence, a new (different) Library. Subsequent attempts to attach instances from our original document to the clip fail, because the clip’s Library no longer matches its original document’s Library. However, if we unload the document in the clip via unloadMovie( ), we regain the ability to attach movies to the clip from its own document Library.
Similarly, loading a .swf file into a clip with loadMovie() prevents us from copying that clip via duplicateMovieClip().
Load movie execution order
The loadMovie() function is not immediately executed when it appears in a statement block. In fact, it is not executed until all other statements in the block have finished executing.
We cannot access an externally loaded movie’s properties or methods in the same statement block as the loadMovie() invocation that loads it into the Player.
Because loadMovie() loads an external file (usually over a network), its execution is asynchronous. That is, loadMovie() may finish at any time, depending on the speed of the file transfer. Therefore, before we access a loaded movie, we should always check that the movie has finished transferring to the Player. We do so with what’s commonly called a preloader—code that checks how much of a file has loaded before allowing some action to take place. Preloaders can be built with the_totalframesand_framesloadedmovie clip properties and the getBytesLoaded() and getBytesTotal() movie clip methods. See the appropriate entries under the MovieClip class in the Language Reference for sample code.
Next: Movie and Instance Stacking Order >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter 13 of the book ActionScript for Flash MX: the Definitive Guide, second edition, written by Colin Moock (O'Reilly; ISBN: 059600396X). Check it out today at your favorite bookstore. Buy this book now.
|
|