From within a Flash SWF file, you can load other external SWF files as assets. There are several reasons to load external SWF files:

  • 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.  
  • It lets you load only the content you need based on what the user is doing in your movie.
  • Multiple SWF files can be played in succession without making the browser load another HTML page. The pages don't have the pause or refresh associated with reloading.
  • It gives you greater flexibility about the organization of your project assets.
  • A complex user interface can be created that does not rely on a single SWF file. Many SWF files can be combined to create the interface. Combining SWF files simplifies editing, because an individual, smaller SWF file can be revised without affecting the other parts of the interface.
  • Multiple authors can collaborate on the same project by working on separate FLA files.

Placing content in a separate SWF file is similar to placing content in its own movie clip symbol. The difference is that the content exists outside the main SWF file. You can also load image files, such as JPG, PNG, GIF.

To load an external SWF file from within another SWF file, use one of the following:

  • The ActionScript 3.0 Loader class
  • The ActionScript 2.0 loadMovie command
  • The ActionScript 2.0 MovieClipLoader class

Using the ActionScript 3.0 Loader class

The Loader class in ActionScript 3.0 is a subclass of DisplayObject that you use to load and display external content. To load the SWF file, you use the load method of the class. The load method has one required parameter, an URLRequest instance containing the URL address of the content to load.

The following code example creates a Loader instance and loads an external SWF file named "myExternalMovie.swf."

var myLoader:Loader = new Loader();                     // create a new instance of the Loader class
var url:URLRequest = new URLRequest("ExternalSWF.swf"); // in this case both SWFs are in the same folder 
myLoader.load(url);                                     // load the SWF file
addChild(myLoader);                                     // add that instance to the display list, adding it to the Stage at 0,0

// (optional)
myLoader.x = 10;                                        // move the loaded SWF 10 pixels to the right (from the left edge)   
myLoader.y = 175;                                       // move the loaded SWF 175 pixels down from the top

// (optional) load a second external SWF file
var my2ndLoader:Loader = new Loader();
var url2:URLRequest = new URLRequest("ExternalSWF2.swf");
my2ndLoader.load(url2);
addChild(my2ndLoader);                                  // optionally, you could put the 2nd SWF beneath 
                                                        // the 1st by using addChildAt(my2ndLoader, 1);
                                                        // displacing the 1st SWF from position 1 to 2 in the display list

// (optional) scaling of the 2nd SWF file
my2ndLoader.scaleX = 2;                                 // scale the SWF horizontally by 200%
my2ndLoader.scaleY = 2;                                 // scale the SWF vertically by 200%

The URL of the SWF file being loaded can be relative or absolute. See Relative Paths below for more information regarding how Flash Player handles URLs. For more information about the Loader class, see Loader in the Platform ActionScript Language Reference.

References to root, when available, always reflect the top-most display object in the portion of the display list's tree structure that SWF file represents. (For images, root references the Bitmap object.)

Note: In ActionScript 3.0, there is no equivalent to the ActionScript 2.0 _lockroot or _level property. See Basics of display programming in the ActionScript 3.0 Developer's Guide.

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 parent movie in ActionScript 2.0.

The loadMovie command has two different forms:

  • MovieClip.loadMovie method: The MovieClip method is used for loading external content into a specific movie clip instance.
  • Global loadMovie function: 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 variation loads content into movies or levels and the second (loadMovieNum) loads specifically into levels.

MovieClip.loadMovie

When loading external content into movie clip instances, Adobe recommends that you use the MovieClip method version of loadMovie. This version is called directly from the movie clip that you want to load the content into and is passed the URL of the content.

myMovieClipInstance.loadMovie("myExternalMovie.swf");  // here only the filename is given, indicating the SWF file 
                                                       // is in the same folder as the parent SWF.

The URL of the content being loaded can be relative or absolute. See Relative Paths below for more information regarding how the Flash player handles URLs.

When loaded, the content is displayed within the container movie clip. The location as well as other basic properties of the container movie clip are retained. However, any custom properties or functions defined within the container movie clip are no longer present. The new content replaces all previous content (including code and event handlers like onRelease). Therefore, any attempts at using a onLoad event handler for the movie clip can't work. In this case, use the MovieClipLoader class instead (see below). For more information on MovieClip.loadMovie, see MovieClip.loadMovie in the ActionScript 2.0 Language Reference.

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 loaded. The target parameter can be either a string or a reference. The following lines are equivalent to loading "myExternalMovie.swf" into the movie clip instance called myContainer:

loadMovie("myExternalMovie.swf", myContainer);    // the target myContainer is an object reference
loadMovie("myExternalMovie.swf", "myContainer");  // the target "myContainer" is a string

loadMovie can also load content into different levels of the Flash player. Levels in Flash player are like player layers. Multiple movies can be played in the same instance of Flash player without being nested inside one another. Each level represents a unique root where movies can play independently of movies within other levels (using _lockroot is unnecessary).

You can reference levels in ActionScript using _level followed by a number representing the level number. The first movie loaded into Flash Player is on _level0. Additional levels can be added on top of that level. 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 method is just like loadMovie except that it only targets levels and it targets them by number, not by name. To load an external SWF file into level 1 (_level1) for example, use the following:

loadMovieNum("myExternalMovie.swf", 1);

When loading into levels, Adobe recommends that you use loadMovieNum over loadMovie. For more information, see global loadMovie in the ActionScript 2.0 Language Reference.

Use _lockroot to prevent _root conflicts

When loading an external movie into another movie, the _root reference of the loaded movie clip changes from its main timeline to the timeline of the movie that loaded it. In other words,  _root always references to the top-most timeline in the hierarchy. If you don't want _root to reference the top-most timeline, set the _lockroot property of the main timeline of the loaded movie clip to true. This property tells all children of that timeline that when they reference _root, to reference that timeline.

this._lockroot = true;     // add this code in the main timeline of the SWF file that will be loaded into another SWF

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. Callbacks like onLoad aren't possible. However, the MovieClipLoader circumvents this restriction by working as a surrogate to such events. You create separate MovieClipLoader instances to manage the loading of content into another movie clip. Therefore, clearing of variables or functions within that movie clip doesn't occur.

When loading content into a movie clip through the MovieClipLoader class, first make a new instance of the class. Then use loadClip to load content into a target movie clip. In the following example, the new content is being loaded into the movie clip myContainer.

var myLoader:MovieClipLoader = new MovieClipLoader();
myLoader.loadClip("myExternalMovie.swf", myContainer);

If you want to know that the content is loaded, use an onLoadInit event handler with your MovieClipLoader instance.

var myLoader: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, use the MovieClipLoader class instead of MovieClip.loadMovie. (For example, use this event handler when you want to be able to check for loading progress.) For more information on the MovieClipLoader class, see MovieClipLoader in the ActionScript 2.0 Language Reference.

Note: MovieClipLoader class is only available when publishing to Flash Player 7 or later.

 Use relative paths to load content

Using relative paths with Loader and loadMovie can be confusing. Because the timeline of any SWF file or movie clip can perform a loadMovie action, ask "what timeline 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. See Relative URLs not referenced correctly | Flash (tn_04157) for a discussion of relative paths, which is relevant to loading external SWF files as well.

Frame rate considerations

In most cases, a loaded movie inherits the parent movie's frame rate. For example, a SWF file whose frame rate is 12 fps plays back at 24 fps when loaded into a movie whose frame rate is 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 does the main movie then inherit the frame rate of the loaded movie 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.

Keywords: Flash Player, load movie; ActionScript; scripting; levels; tell target; variables; target; instance; SWF; loadMovie; tn_14190

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy