How to load external SWF content
What's covered
- Using the ActionScript 2.0 loadMovie command
- Using the ActionScript 2.0 MovieClipLoader class
- Using the ActionScript 3.0 Loader class
Flash movies allow you to load external SWF files (or image files including JPG, PNG, or GIF) into the Adobe Flash Player to play along with the content of the original. There are several advantages to loading external movies into the Flash Player instead of using a larger, single SWF file.
- Multiple SWF files can be played in succession without making the browser load another HTML page. This avoids the pause or refresh of the page associated with reloading.
- Because a project can be broken up into multiple movies, the individual SWF files are smaller in size. Smaller files load faster, and manage memory more efficiently.
- A complex Flash interface can be created that does not rely on a single Flash file. Many files can reference the same interface file. This simplifies editing, because an individual, smaller SWF file can be revised without affecting the other movies. Multiple authors can collaborate on the same visual interface.
In ActionScript 2.0, you use the loadMovie command or the MovieClipLoader class. In ActionScript 3.0, you use the Loader class.
Using the ActionScript 2.0 loadMovie command
The loadMovie command loads an external SWF file or image into a MovieClip or another level of the player in ActionScript 2.0. The loadMovie command comes in two different forms, one as a MovieClip method and the other as a global function. The MovieClip method is used for loading external content into specific movie clip instances while the global loadMovie function can be used to load content into movies or levels. The global version also has two variations, loadMovie and loadMovieNum , the first for loading content into movie clips or levels and the second (loadMovieNum) for loading specifically into levels.
MovieClip.loadMovie
When loading external content into movie clip instances, it's recommended that you use the MovieClip method version of loadMovie. This is called directly from the movie clip you wish to load the content into and is passed the URL of the content.
myContainer.loadMovie("myExternalMovie.swf");
The URL of the content being loaded can be relative or absolute. See the Additional Information section for more information regarding how the Flash player handles URLs. When loaded, the content will be displayed within the container movie clip. The location of that movie clip as well as other basic properties will be retained; however, any custom properties or functions defined within that movie clip will no longer be present as a result of the new content replacing all previous content (including code and event handlers like onRelease). This means any attempts at using a onLoad event handler for the movie clip will not work. Instead you should use the MovieClipLoader class. For more information on MovieClip.loadMovie, see MovieClip.loadMovie on LiveDocs.
global loadMovie and loadMovieNum
The loadMovie command also exists as a global function. This function has two required parameters, the URL of the external content and the target in which the content is to be loaded. The target parameter can be either a string or a reference. The following lines are equivalent to loading "myExternalMovie.swf" into the myContainer movie clip:
loadMovie("myExternalMovie.swf", myContainer); loadMovie("myExternalMovie.swf", "myContainer");
loadMovie is also capable of loading content into different levels of the Flash player. Levels in the Flash player are like player layers where multiple movies can be played in the same instance of the Flash player without being inside one another. Each level represents a unique root where movies can play independently of movies within other levels (making use of _lockroot unnecessary).
You can reference levels in ActionScript using _level followed by a number representing the layer number. The first movie loaded into the player is on level 0 or _level0. Additional levels can be added on top of that. The following call to loadMovie loads "myExternalMovie.swf" into level 1 on top of the current movie playing in the player.
loadMovie("myExternalMovie.swf", "_level1");
A variation of the global loadMovie function is loadMovieNum. This is just like loadMovie except that it only targets levels and it targets levels by number, not name. To load an external SWF into level 1 (_level1) for example, you would use:
loadMovieNum("myExternalMovie.swf", 1);
When loading into levels, it is recommended that you use loadMovieNum over loadMovie. For more information, see global loadMovie on LiveDocs.
Using _lockroot to prevent _root conflicts
When loading an external movie into a movie clip of another movie, the _root reference of the loaded movie clip changes from it's main timeline to the timeline of the movie that loaded it, since _root always references to the top-most timeline in the hierarchy. If you would like to prevent this from happening, you can set the _lockroot property of the main timeline of the loaded movie clip to true. This tells all children of that timeline that when they reference _root, they should reference that timeline and not the _root of the movie in which that movie was loaded.
// in the main timeline of the movie to be loaded this._lockroot = true;
Note: The _lockroot property is only available when publishing to Flash Player 7 or later.
Using the ActionScript 2.0 MovieClipLoader class
The MovieClipLoader class in ActionScript 2.0 is designed to make the process of loading external content into MovieClip instances easier. As mentioned earlier, variables and functions defined in movie clips are removed when new content is loaded into those movie clips preventing callbacks like onLoad from being possible. However, the MovieClipLoader circumvents this by working as a surrogate to such events. Since you create separate MovieClipLoader instances to manage the loading of content into another movie clip, it is not affected by the clearing of variables of functions within that movie clip.
When loading content into a movie clip through the MovieClipLoader class, you need to first make a new instance of the class, then use loadClip to load content into a target movie clip, in the following example, myContainer.
var myLoader:MovieClipLoader = new MovieClipLoader(); myLoader.loadClip("myExternalMovie.swf", myContainer);
To know when the content is loaded, you can use an onLoadInit event handler with your MovieClipLoader instance.
var mycLoader:MovieClipLoader = new MovieClipLoader(); myLoader.addListener(this); myLoader.loadClip("myExternalMovie.swf", myContainer); function onLoadInit(mc:MovieClip) { trace("content has been loaded into "+mc); }
When you want to have more control over the information regarding the loading of content into a movie clip such as loading progress and knowing when the content has been loaded, it is recommended you use the MovieClipLoader class over MovieClip.loadMovie. For more information on the MovieClipLoader class, see MovieClipLoader on LiveDocs.
Note: MovieClipLoader class is only available when publishing to Flash Player 7 or later.
Using the ActionScript 3.0 Loader class
The Loader class in ActionScript 3.0 is a subclass of DisplayObject that can be used to load and display external content. It has a load method that works much the same way as the ActionScript 2.0 MovieClip.loadMovie command. The load command has one required parameter, a URLLoader instance with the URL of the content to be loaded.
The following example creates a Loader instance and loads into that instance an external movie named "myExternalMovie.swf".
var myLoader:Loader = new Loader(); addChild(myLoader); var url:URLRequest = new URLRequest("myExternalMovie.swf"); myLoader.load(url);
The URL of the content being loaded can be relative or absolute. See the Additional Information section for more information regarding how the Flash player handles URLs. Additional information can be obtained for the content being loaded using event listeners. For more information on the MovieClipLoader class, see Loader on LiveDocs.
Note: There is no equivalent _lockroot property in ActionScript 3.0. References to root, when available, always reflect the top-most display object in the portion of the display list's tree structure represented by that SWF file (for images, root references the Bitmap object).
Note: There are no levels when dealing with ActionScript 3.0.
Additional Information
Relative Paths
Using relative paths with Loader and loadMovie can be confusing. Since any timeline can perform a load movie action, what path is the movie being loaded relative to? Is it relative to the main timeline at _level0? Or is it relative to the timeline that performed the movie-loading action? The answer is simple: Loaded movies are always relative to the timeline that loaded them. Please refer to "Using Relative URLs with Get URL Action" (TechNote 4157) for a discussion of relative paths, which contains information that is relevant to loading movies.
Frame rate
Loaded movies also, in most cases, inherit the parent movie's frame rate. For example, a SWF file whose frame rate is 12 fps, when loaded into a movie whose frame rate is 24 fps, will play back at 24 fps. The only exception is if the movie being loaded contains a sound on the timeline whose sync is set to "stream". Only then will the main movie then inherit the frame rate of the loaded movie as to assure the correct playback of that sound.
Note: ActionScript 3.0 allows you to change the frame rate dynamically using the Stage.frameRate property.
This content requires Flash
To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.
Download the free Flash Player now!
