title: AS3: 1180 Call to a possibly undefined property: Add Child
body: <p>Pretty big noob at AS3. I'm trying to Load a SWF into another file. (Main .fla loading external SWFS on mouse click)</p>
  
  <p>I keep getting the error: 1180 Call to a possibly undefined property: Add Child, however.</p>
  
  <p>My LoadSWF code is:</p>
  
  <pre><code>package {
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  
  
  
  public class LoadSWF {
  
      private var loaderFile: Loader;
      private var swfFile: Object;
      private var sourceFile: String;
      private var fileLabel: String;
      private var canDrag: Boolean;
      private var popX: int;
      private var popY: int;
  
  
  
  
      public function LoadSWF(myFile: String, myLabel: String, myX: int = 0, myY: int = 0, myDrag: Boolean = false) {
          // constructor code
          trace("Loading SWF file:", myFile);
          sourceFile = myFile;
          fileLabel = myLabel;
          canDrag = myDrag;
          popX = myX;
          popY = myY;
          openSWF();
      }
  
      private function openSWF(): void {
          // initialise variables      
          loaderFile = new Loader();
          swfFile = addChild(loaderFile);
          // set initial position of the SWF popup  
          loaderFile.x = popX;
          loaderFile.y = popY;
          try {
              loaderFile.load(new URLRequest(sourceFile));
              // runs after the SWF popup has finished loading     
              loaderFile.contentLoaderInfo.addEventListener(Event.COMPLETE, SWFloaded);
          } catch (err: Error) {
              // provide some error messages to help with debugging      
              trace("Error loading requested document:", sourceFile);
              trace("Error:", err);
              sourceFile = null;
          }
      } //end openSWF
  
      private function SWFloaded(evt: Event): void {
          // and add the required event listeners      
          loaderFile.addEventListener(MouseEvent.MOUSE_DOWN, dragSWF);
          loaderFile.addEventListener(MouseEvent.MOUSE_UP, dropSWF);
          // use the POPUP reference to access MovieClip content    
          swfFile.content.gotoAndStop(fileLabel);
          // assigns a close function to the button inside the popup    
          swfFile.content.closeBtn.addEventListener(MouseEvent.CLICK, closeSWF);
          // remove the COMPLETE event listener as it’s no longer needed      
          loaderFile.contentLoaderInfo.removeEventListener(Event.COMPLETE, SWFloaded);
      } //end SWFLOaded   
  
  
      private function dragSWF(evt: MouseEvent): void {
          swfFile.content.startDrag();
      }
      private function dropSWF(evt: MouseEvent): void {
          swfFile.content.stopDrag();
      }
      private function closeSWF(evt: MouseEvent): void {
          // remove the required event listeners first    
          loaderFile.removeEventListener(MouseEvent.MOUSE_DOWN, dragSWF);
          loaderFile.removeEventListener(MouseEvent.MOUSE_UP, dropSWF);
          swfFile.content.closeBtn.removeEventListener(MouseEvent.CLICK, closeSWF);
          // remove the pop-up    
          loaderFile.unloadAndStop();
      }
  
  
  } //end class
  </code></pre>
  
  <p>} //end package</p>
  
  <p>I'm calling it into my main class file, which code is (I left out the rest, guessing its unnecessary):</p>
  
  <pre><code>package {
  
  import flash.display.MovieClip;
  import flash.text.TextField;
  import flash.display.SimpleButton;
  import flash.utils.Dictionary;
  import flash.text.TextFormat;
  import flash.net.*;
  import flash.events.*;
  import fl.controls.*;
  import flash.media.*;
  import fl.events.ComponentEvent;
  import fl.managers.StyleManager;
  import fl.data.DataProvider;
  import fl.data.SimpleCollectionItem;
  import fl.managers.StyleManager;
  import fl.events.ComponentEvent;
  import flash.events.Event;
  import flash.net.SharedObject;
  import LoadSWF;
  
  public class Main extends MovieClip {
  
      //Declare variables
      private var componentFmt: TextFormat;
      private var radioBtnFmt: TextFormat;
      private var playerData: Object;
      private var savedGameData: SharedObject;
      // Pop-up Variables     
      private var popupFile: LoadSWF;
      private var swfPath: String;
  
  
  
  
      public function Main() {
          // constructor code
          this.savedGameData = SharedObject.getLocal("savedPlayerData");
          this.setComponents();
          this.setPlayerData();
          //this.swfPath = "";
          //this.isHelpOpen = false;
          //this.sndPath = "musicSFX/music2.mp3";
          //this.isMuted = false;
          //this.sndTrack = new LoadSND(this.sndPath, this.canRepeat);;
          //this.muteBtn.addEventListener(MouseEvent.CLICK, this.setMute);
          swfPath = "";
          helpBtn.addEventListener(MouseEvent.CLICK, openSWF);
          //hauntedForestBtn.addEventListener(MouseEvent.CLICK, openSWF);
      }
  
  
  
      public function openSWF(evt: MouseEvent): void {
          // determine which button was pressed  
          var myFile: String = evt.currentTarget.name.replace("Btn", ".swf");
          myFile = (swfPath == "") ? myFile : swfPath + myFile;
          if (myFile == "help.swf") {
              // load the help SWF file - is draggable  
              swfFile = new LoadSWF(myFile, currentFrameLabel, 80, 60, true);
          } else {
              // load the selected SWF file - is not draggable        
              swfFile = new LoadSWF(myFile, currentFrameLabel);
          }
          addChild(swfFile);
      }
  </code></pre>
  
  <p>If anyone could help me find a solution, i'd greatly appreciate it.</p>
  
  <p>Thanks :) </p>