How to Load One SWF to Another

Flash files can load external media items such as videos and other Flash files.
... Hemera Technologies/AbleStock.com/Getty Images

You can use ActionScript 3.0 code within Flash files to carry out import operations, including loading external resources. With the ActionScript 3.0 Loader class, you can bring a media item into an SWF for display to the user. The process can be applied to external SWF files, so you can load one Flash movie into another, with the second one loaded while the user is viewing the first one.

1 Loader Class

To load an external SWF file into an SWF file you are working on, use the ActionScript 3.0 Loader class. With the file open in the Flash Professional software, you can add your ActionScript code to a dedicated layer, writing your code into the Actions panel. The Loader class allows you to load and manage external resources in a Flash file. The following example code demonstrates creating a Loader object:

var swfLoader:Loader = new Loader();

2 URLRequest Class

The URLRequest class works in conjunction with the Loader class in ActionScript 3.0 to load files into Flash. The following sample code demonstrates creating a URLRequest object:

var swfFile:URLRequest = new URLRequest("filename.swf");

The parameter to the URLRequest constructor must reflect both the name and location of the SWF file being loaded. In this case the file being loaded is in the same location as the file loading it. If the file is in a different location, the URLRequest object must receive the full or relative path to its location as well as its name.

3 Loading the File

ActionScript 3.0 code allows Flash files to load one SWF into another by passing a URLRequest object to the load method of the a Loader object. The following code demonstrates the syntax:

swfLoader.load(swfFile);

In this case the "swfLoader" object must be an existing instance of the Loader class and the "swfFile" must be an instance of the URLLoader object. The Loader will load the resource at the location encapsulated by the URLRequest.

4 Adding the SWF to the Stage

The Loader and URLRequest classes handle loading an SWF into Flash, but the SWF will not automatically be displayed within the visual file presented to the user. To display the externally loaded SWF file within the visible stage area, ActionScript 3.0 provides the "addChild" method, as the following code demonstrates:

addChild(swfLoader);

By default, the loaded SWF will appear at zero "X" and "Y" positions, but you can change this via additional ActionScript code, if necessary.

Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English language and literature, as well as a Master of Science in information technology, both from the University of Glasgow.

×