How to Play a SWF Inside a SWF
Flash makes combining different items of media easy. Importing and playing an external SWF inside Flash movies is a common and useful task. It allows you to make use of other resources and to separate the different elements in a Flash application, minimizing the download times and ease of use for people viewing your work. To play a SWF file inside another SWF, you need only a few simple lines of ActionScript code that can be easily inserted and tailored to suit your needs.
Instructions
-
-
1
Create your Flash file and set it up for loading the second SWF file. Add any visual or interactive features you want using the Flash drawing tools and components. Set your document properties to create the dimensions you need. Insert a new layer in your Flash file by pressing the "Insert Layer" button in the Timeline area, naming the layer "actions" to keep your code in one place.
-
2
Add code to your Flash file by clicking the "actions" layer and opening the Actions panel, inserting the following and substituting the name of the external SWF you want loaded.
If you're using ActionScript 3.0:
var movieLoader:Loader = new Loader();
addChild(movieLoader);
var movieURL:URLRequest = new URLRequest("second_swf_name.swf");
movieLoader.load(movieURL);
For ActionScript 2.0:
var movieLoader:MovieClipLoader = new MovieClipLoader();
var holdingClip_mc:MovieClip = _root.createEmptyMovieClip("holdingClip_mc", _root.getNextHighestDepth());
movieLoader.loadClip("second_swf_name.swf", holdingClip_mc);
-
-
3
Set up your ActionScript code to check when the SWF has been completely loaded.
In ActionScript 3.0:
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);
function swfComplete(completeEvent:Event):Void
{ trace("SWF Completely Loaded");
//carry out any additional tasks here
movieLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfComplete); }
In ActionScript 2.0, the following code must be inserted before the SWF is loaded (which is the line with "loadClip" in it):
var swfLoadListener:Object = new Object();
swfLoadListener.onLoadComplete = function(target_mc:MovieClip)
{ trace("SWF Completely Loaded");
//carry out any additional tasks here
movieLoader.removeListener(swfLoadListener); };
movieLoader.addListener(swfLoadListener);
This allows you to detect when the SWF is in, so your code can move on with any additional processing tasks you want to carry out.
-
4
Indicate the status of loading to your users. People are more likely to wait for resources to load if you indicate status as loading progresses.
For ActionScript 3.0:
movieLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showStatus);
function showStatus(pEvent:ProgressEvent):void
{ indicator_txt.text = "Loading movie: "+(pEvent.bytesLoaded/pEvent.bytesTotal*100)+"%"; }
For ActionScript 2.0:
swfLoadListener.onLoadProgress = function(target_mc:MovieClip, numBytesLoaded:Number, numBytesTotal:Number)
{ indicator_txt.text = "Loading movie: "+(numBytesLoaded/numBytesTotal*100)+"%"; };
Add a dynamic text field to the stage area in your Flash file, typing the Instance Name "indicator_txt" into the box on the Properties panel. Position the text field to be visible while the SWF is loading.
-
5
Export your Flash file and upload it to a Web server. Test it to see that the second SWF loads, and that the status is updated correctly. If your SWF does not load correctly, check that the URL is correct, remembering that the location you specify will be relative to where your main SWF is situated. If your second SWF is on another server, you will need to use an Absolute URL indicating the full path to the movie file, as in the following example:
http://swfdomain.com/swfname.swf
-
1
Tips & Warnings
Try using a creative approach to indicate the load progress of your SWF file. Instead of simply indicating a percentage in text, consider using a visual indicator such as those provided by Flash, or alternatively create your own.
Never assume that, because an SWF file works on one browser, it will work on any browser. When you load external resources into a SWF, its behavior can change drastically between different browsers, so make sure you test it properly.
References
Resources
- Photo Credit Stockbyte/Stockbyte/Getty Images