A friend of mine sent me an email asking how to import compiled (.swf) movie clips into Adobe Flash on the fly. Luckily, it’s quite simple, so I decided to write a quick article on it.
In Actionscript 3.0 (the version he is using) you can use the Loader class. The Loader class only needs a URLRequest to import the file you want.
In the following example I’m going to want to import a compiled file called Child.swf that is in the same directory as the one we are running this from.
-
myLoader.load(new URLRequest("Child.swf"));
-
addChild(myLoader);
If you compile this, it will import the Child.swf directly to the stage. We can also adjust where the clip gets positioned by using the x and y variables of the myLoader instance.
-
myLoader.y = 100;
Be careful though, just because we added it to the stage does not mean it has loaded yet. By adding an Event Listener of type Event.COMPLETE, we can be notified when the loading is finished. Normally you would think to add the Event Listener to the myLoader object itself, but not in this case. Instead you need to attach it to the contentLoaderInfo property as shown below.
-
Event.COMPLETE, loadComplete);
-
-
function loadComplete(event:Event):void {
-
trace("Parent: Load complete!");
-
}
Finally, if we want to interact with the internal operations or variables of that external Flash file (only if it is in our security sandbox – i.e. same domain) you must communicate with the attached child not the Loader object itself. Assuming this is the code inside our Child.swf:
-
We can access this from the <em>Parent.swf</em> file (after load is complete of course) by doing the following:
-
<pre lang="actionscript3">trace("Parent: Child's internal variable is "
-
+ MovieClip(myLoader.getChildAt(0)).theVariable);
Notice how we cast the Loader's child as a MovieClip - if we don't, an error will be thrown about an unknown property: theVariable.
So that's all there is to loading an external compiled Flash file into another.
Here is a quick summary of what we did here - our Child.swf file should contain:
-
And our <em>Parent.swf</em> file:
-
<pre lang="actionscript3">var myLoader:Loader = new Loader();
-
myLoader.contentLoaderInfo.addEventListener(
-
Event.COMPLETE, loadComplete);
-
myLoader.load(new URLRequest("Child.swf"));
-
addChild(myLoader);
-
-
myLoader.x = 150;
-
myLoader.y = 100;
-
-
function loadComplete(event:Event):void {
-
trace("Parent: Load complete!");
-
trace("Parent: Child's internal variable is "
-
+ MovieClip(myLoader.getChildAt(0)).theVariable);
-
}
You can download the source .fla files here (.zip - 10kb).
I hope you enjoyed this little tutorial. Please feel free to comment or email me if you have any questions.
Side Note - Same Directory
I have to mention that the relative path of the loaded swf has to be in relation to the page calling it. So imagine a directory structure like this:
- index.html
- images/Parent.swf
- images/Child.swf
If the index.html file loads images/Parent.swf, then the URLRequest you pass into Loader.load has to be images/Child.swf not Child.swf. This is very important!

