title: Loading and using SWF files
body: <p>I'm new to AS3, and am trying to understand how externally loaded SWFs work in AS3. Since Flash 4/5, it was common to have one main SWF file in a Flash web project, and then load other SWF files into it, often for various "sections" of a website or web project. In the main file, we'd have masks animating the container movieclip(in which external sections/SWF files were loaded) and have animations and transitions play as the section finished loading and the loaded content was displayed. </p>
  
  <p>In AS3, I've used the Loader class to load and display the external file, <strong>my main problem is in communicating with the loaded content, call it's functions, or call root functions from it.</strong></p>
  
  <p>In AS2, we could use someMovieClip.loadMovie("ExternalContent.swf") and the ExternalContent file would load inside someMovieClip. You could access functions on the "External.swf" main timeline using someMovieClip.function();. And inside the "ExternalContent.swf", we could use _root.function() to access functions in the main file ExternalContent was being loaded into. Doing this in AS3 seems bizarre and neurotic, and I feel like I'm missing something fairly basic here.</p>
  
  <pre><code>//Loading in ExternalContent.swf into the sprite
  //ExternalContent has a movieclip called "boxes" on it's main timeline
  //boxes has a boxesPrompt() function in it's timeline.
  
  var sprite:Sprite = new Sprite();
  addChild(sprite);
  
  var loader:Loader = new Loader();
  loader.load(new URLRequest("ExternalContent.swf"));
  
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
  
  function onLoaded(event:Event):void
  {
      sprite.addChild(event.target.content);
  
          sprite.boxes.boxesPrompt();
          //Flash gives the following compiler error at the above
          //Scene 1, Layer 'Layer 1', Frame 1, Line 21 1119: Access of possibly undefined property boxes through a reference with static type flash.display:Sprite.
  
          //But when I comment out sprite.boxes.boxesPrompt() and use this, it works:
          event.target.content.boxes.boxesPrompt()
  }
  </code></pre>
  
  <p>The boxesPrompt() function inside the "ExternalContent.swf" just traces it's parent, grand-parent, and great grand-parent - trace(this.parent.parent.parent);. And when I call that function inside the onLoaded event-handler using "event.target.content.boxes.boxesPrompt()", it shows that the Boxes object(which was on the main timeline of External.SWF), has a parent movieclip, a grand-parent sprite, and a great grand-parent object mainTimeline.</p>
  
  <p>I thought re-parenting the loaded content into the sprite would allow me to access the loaded content as easily as loadMovie() used to be - accessing loaded content like it was present directly inside the clip it was loaded in. But that doesn't work at all.</p>
  
  <p>So to rephrase, my question is:</p>
  
  <ul>
  <li><p>How do I communicate from the main "loader" SWF file, with the content that's loaded in. I don't want to communicate using event.target.content.{etc} because then I'd only be able to address the loaded content inside the Loader's event.complete event handler. </p></li>
  <li><p>How do I "re-parent" loaded content, so I can place it inside some movieclip/sprite on the main timeline of the loader file, rather than using some really long convoluted way.</p></li>
  <li><p>How to communicate from inside the loaded content, to the main/loader file. Previously, we'd use _root.functionName() to do stuff like play some animation transitioning from the current externally loaded "section" to another section. How'd I go about doing that. </p></li>
  </ul>