title: ActionScript / Flash - Loading External, Programatically Created SWF?
body: <p>i've never had to do any cross scripting until now and i'm running into a (probably really stupid) error right at the start.</p>
  
  <p><strong>External SWF:</strong>
  i've created a new ActionScript 3.0 project in Flash Professional CS5.  on the first frame i've added the following script:</p>
  
  <pre><code>//Square.fla frame script
  
  import flash.display.Shape;
  import flash.events.Event;
  
  var s:Shape = new Shape();
  s.graphics.beginFill(0x0000FF, 1.0);
  s.graphics.drawRect(-100, -100, 200, 200);
  s.graphics.endFill();
  
  s.x = stage.stageWidth / 2;
  s.y = stage.stageHeight / 2;
  
  addChild(s);
  
  addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
  
  function enterFrameEventHandler(evt:Event):void
  {
      s.rotation += 2;
  }
  </code></pre>
  
  <p>save, compile, done.  this works fine as a stand alone swf, which simply displays a rotating blue square at center stage.</p>
  
  <p><strong>Main SWF:</strong>
  i've created a new ActionScript 3.0 file in Flash Professional CS5, which has a document class called <em>CrossScriptTest</em>:</p>
  
  <pre><code>//CrossScriptTest.as
  
  package
  {
  //Imports
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import flash.display.Sprite
  import flash.display.Loader;
  import flash.events.IOErrorEvent;
  import flash.net.URLRequest;
  import flash.events.Event;
  
  //Class
  [SWF(width = "1000", height = "500", backgroundColor = "0x444444")]
  public class CrossScriptTest extends Sprite
  {
      //Constants
      private static const SQUARE_SWF_URL:String = "Square.swf";
  
      //Variables
      private var SWFLoader:Loader;
  
      //Constructor
      public function CrossScriptTest()
      {
          stage.scaleMode = StageScaleMode.NO_SCALE;
          stage.align = StageAlign.TOP_LEFT;
          stage.frameRate = 60;
  
          init();
      }
  
      //Initialize
      private function init():void
      {
          SWFLoader = new Loader();
          SWFLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
          SWFLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
          SWFLoader.load(new URLRequest(SQUARE_SWF_URL));
      }
  
      //IOError Event Handler
      private function IOErrorEventHandler(evt:IOErrorEvent):void
      {
          trace(evt);
      }
  
      //Loader Complete Event Handler
      private function loaderCompleteEventHandler(evt:Event):void
      {
          evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
          evt.currentTarget.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);
  
          var squareSWF:Sprite = Sprite(evt.currentTarget.content);
          addChild(squareSWF);
      }
  }
  }
  </code></pre>
  
  <p><strong>Error:</strong>
  i receive the following error:</p>
  
  <blockquote>
    <p>TypeError: Error #1009: Cannot access a property or method of a null
    object reference.     at Square_fla::MainTimeline/frame1()</p>
  </blockquote>
  
  <p>perhaps i'm misunderstanding the nature of cross scripting or loading external swf files, but i can only seem to make this work if i've manually drawn display objects on the stage and not if the external swf's display objects are generated by code.</p>
  
  <p>is it not possible to load external swfs that are programatically created and add them to the display list of a main swf?</p>