Loading External Flash Movies

Tutorials

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.

var myLoader:Loader = new Loader();
  1. myLoader.load(new URLRequest("Child.swf"));
  2. 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.x = 150;
  1. 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.

myLoader.contentLoaderInfo.addEventListener(
  1.   Event.COMPLETE, loadComplete);
  2.  
  3. function loadComplete(event:Event):void {
  4.   trace("Parent: Load complete!");
  5. }

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:

var theVariable:int = 15;
  1. We can access this from the <em>Parent.swf</em> file (after load is complete of course) by doing the following:
  2. <pre lang="actionscript3">trace("Parent: Child's internal variable is "
  3.   + 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:
 

var theVariable:int = 15;
  1. And our <em>Parent.swf</em> file:
  2. <pre lang="actionscript3">var myLoader:Loader = new Loader();
  3. myLoader.contentLoaderInfo.addEventListener(
  4.   Event.COMPLETE, loadComplete);
  5. myLoader.load(new URLRequest("Child.swf"));
  6. addChild(myLoader);
  7.  
  8. myLoader.x = 150;
  9. myLoader.y = 100;
  10.  
  11. function loadComplete(event:Event):void {
  12.   trace("Parent: Load complete!");
  13.   trace("Parent: Child's internal variable is "
  14.     + MovieClip(myLoader.getChildAt(0)).theVariable);
  15. }

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!

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • My Lifestream

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.