title: Error #2025: The supplied DisplayObject must be a child of the caller - Not sure how to fix
body: <p>So I get an error saying that the supplied DisplayObject must be a child of the caller. What happens is my game works first time around in that clicking the <code>'Play'</code> button calls the <code>startGame</code> function and removes the menu so that the game is shown, but then at the end of the game when the <code>playAgainBtn</code> is clicked, instead of simply playing the game again / restarting the game, I get this error:</p>
  
  <p><code>ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
      at flash.display::DisplayObjectContainer/removeChild()</code></p>
  
  <p>It specifically points to this line:</p>
  
  <p><code>menuLayer.removeChild(mainMenu);</code></p>
  
  <p>Here is the code:</p>
  
  <pre><code>package  {
      import flash.display.MovieClip;
      import flash.display.LoaderInfo;
      import flash.display.Sprite;
      import flash.display.DisplayObject;
      import flash.display.Stage;
      import flash.events.Event;
      import flash.events.MouseEvent;
      import flash.events.TimerEvent;
      import flash.utils.Timer;
      import flash.ui.Mouse;
      import flash.media.Sound;
      import flash.media.SoundChannel;
      import flash.text.TextFormat;
      import flash.text.TextField;
      import flash.text.AntiAliasType;
      import flash.display.Loader;
      import flash.net.URLRequest;
      import flash.net.URLLoader;
      import flash.system.LoaderContext;
      import flash.display.Sprite;
      import flash.net.Socket;
      import caurina.transitions.Tweener;
      import flash.text.Font;
  
      public class Main extends MovieClip {
  
      public static var backgroundLayer:Sprite = new Sprite;
      public static var gameLayer:Sprite = new Sprite;
      public static var interfaceLayer:Sprite = new Sprite;
      public static var endGameLayer:Sprite = new Sprite;
      public static var menuLayer:Sprite = new Sprite;
      public static var gameOverLayer:Sprite = new Sprite;
      public static var howToLayer:Sprite = new Sprite;
      public static var scoresLayer:Sprite = new Sprite;
      public static var aboutLayer:Sprite = new Sprite;
  
      public var mainMenu:menuMain = new menuMain;
      public var gameEnd:endGame = new endGame;
      public var howtoPlay:howToPlay = new howToPlay;
      public var gameAbout:aboutGame = new aboutGame;
      public var intro:IntroSound = new IntroSound();
      public var soundControl:SoundChannel = new SoundChannel();
  
      public var gameTime:int;
      public var levelDuration:int;
  
      public var crosshair:crosshair_mc;
      static var score:Number;
  
      var enemyShipTimer:Timer;
      var enemyShipTimerMed:Timer;
      var enemyShipTimerSmall:Timer;
  
      static var scoreHeader:TextField = new TextField();
      static var scoreText:TextField = new TextField();
      static var timeHeader:TextField = new TextField();
      static var timeText:TextField = new TextField();
  
      static var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
      public var gameOverscoreFormat = new TextFormat("Arial Rounded MT Bold", 32, 0xFFFFFF);
  
     public function Main()
     {
          addChild(gameLayer);
          addChild(backgroundLayer);
          addChild(interfaceLayer);
          addChild(menuLayer);
  
          menuLayer.addChild(mainMenu);
          interfaceLayer.addChild(howtoPlay);
          interfaceLayer.addChild(gameEnd);
          interfaceLayer.addChild(gameAbout);
          soundControl = intro.play(0, 100);
          addMenuListeners();
     }
  
     public function menuReturn(e:Event)
     {
  
          addChild(gameLayer);
          addChild(backgroundLayer);
          addChild(interfaceLayer);
          addChild(menuLayer);
  
          menuLayer.addChild(mainMenu);
          interfaceLayer.addChild(howtoPlay);
          interfaceLayer.addChild(gameEnd);
          interfaceLayer.addChild(gameAbout);
     }
  
     public function showAbout(e:Event)
     {
        menuLayer.removeChild(mainMenu);
        interfaceLayer.addChild(gameAbout);
     }
  
     public function startGame(e:Event)
     {
          removeMenuListeners();
          soundControl.stop();
          interfaceLayer.removeChild(howtoPlay);
          interfaceLayer.removeChild(gameAbout);
          interfaceLayer.removeChild(gameEnd);
          menuLayer.removeChild(mainMenu);
  
          levelDuration = 30;
          gameTime = levelDuration;
          var gameTimer:Timer = new Timer(1000,levelDuration);
          gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
          gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
          gameTimer.start();
  
          scoreHeader = new TextField();
          scoreHeader.text = String("Score: ");
          interfaceLayer.addChild(scoreHeader);
          scoreHeader.x = 5;
          scoreHeader.selectable = false;
          scoreHeader.embedFonts = true;
          scoreHeader.antiAliasType = AntiAliasType.ADVANCED;
  
          scoreText = new TextField();
          scoreText.text = String("0");
          interfaceLayer.addChild(scoreText);
          scoreText.x = 75;
          scoreText.y = 0;
          scoreText.selectable = false;
          scoreText.embedFonts = true;
          scoreText.antiAliasType = AntiAliasType.ADVANCED;
  
          timeHeader = new TextField();
          timeHeader.text = String("Time: ");
          interfaceLayer.addChild(timeHeader);
          timeHeader.x = 500;
          timeHeader.y = 0;
          timeHeader.selectable = false;
          timeHeader.embedFonts = true;
          timeHeader.antiAliasType = AntiAliasType.ADVANCED;
  
          timeText = new TextField();
          timeText.text = gameTime.toString();
          interfaceLayer.addChild(timeText);
          timeText.x = 558;
          timeText.y = 0;
          timeText.selectable = false;
          timeText.embedFonts = true;
          timeText.antiAliasType = AntiAliasType.ADVANCED;
  
          scoreHeader.setTextFormat(scoreFormat);
          scoreText.setTextFormat(scoreFormat);
          timeHeader.setTextFormat(scoreFormat);
          timeText.setTextFormat(scoreFormat);
  
          var timeScorebg:Sprite = new Sprite();
          backgroundLayer.addChild(timeScorebg);
          timeScorebg.graphics.beginFill(0x333333);
          timeScorebg.graphics.drawRect(0,0,600,30);
          timeScorebg.graphics.endFill();
          timeScorebg.y = 0;
  
          enemyShipTimer = new Timer(2000);
          enemyShipTimer.addEventListener("timer", sendEnemy);
          enemyShipTimer.start();
  
          enemyShipTimerMed = new Timer(2500);
          enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
          enemyShipTimerMed.start();
  
          enemyShipTimerSmall = new Timer(2750);
          enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
          enemyShipTimerSmall.start();
  
          crosshair = new crosshair_mc();
          gameLayer.addChild(crosshair);
  
          crosshair.mouseEnabled = crosshair.mouseChildren = false;
  
          Mouse.hide();
  
          gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
          resetScore();
      }
  
      function addMenuListeners():void
      {
          mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
          mainMenu.howToPlayBtn.addEventListener(MouseEvent.CLICK, showInstructions);
          mainMenu.aboutBtn.addEventListener(MouseEvent.CLICK, showAbout);
          howtoPlay.backBtn.addEventListener(MouseEvent.CLICK, menuReturn);
          gameEnd.playagainBtn.addEventListener(MouseEvent.CLICK, startGame);
          gameAbout.backBtn.addEventListener(MouseEvent.CLICK, menuReturn);
      }
  
      function removeMenuListeners():void
      {
          mainMenu.playBtn.removeEventListener(MouseEvent.CLICK, startGame);
          mainMenu.howToPlayBtn.removeEventListener(MouseEvent.CLICK, showInstructions);
          mainMenu.aboutBtn.removeEventListener(MouseEvent.CLICK, showAbout);
          howtoPlay.backBtn.removeEventListener(MouseEvent.CLICK, menuReturn);
          gameEnd.playagainBtn.removeEventListener(MouseEvent.CLICK, startGame);
          gameAbout.backBtn.removeEventListener(MouseEvent.CLICK, menuReturn);
      }
  
      public function showInstructions(e:Event)
      {
          menuLayer.removeChild(mainMenu);
          interfaceLayer.addChild(howtoPlay);
      }
  
      function sendEnemy(e:Event)
      {
          var enemy = new EnemyShip();
          gameLayer.addChild(enemy);
          gameLayer.addChild(crosshair);
      }
  
      function sendEnemyMed(e:Event)
      {
          var enemymed = new EnemyShipMed();
          gameLayer.addChild(enemymed);
          gameLayer.addChild(crosshair);
      }
  
      function sendEnemySmall(e:Event)
      {
          var enemysmall = new EnemyShipSmall();
          gameLayer.addChild(enemysmall);
          gameLayer.addChild(crosshair);
      }
  
      static function updateScore(points)
      {
          score += points;
          scoreText.text = String(score);
          scoreHeader.setTextFormat(scoreFormat);
          scoreText.setTextFormat(scoreFormat);
      }
  
      static function resetScore()
      {
          score = 0;
          scoreText.text = String(score);
          scoreText.setTextFormat(scoreFormat);
      }
  
      function updateTime(e:TimerEvent):void
      {
          gameTime--;
          timeText.defaultTextFormat = scoreFormat;
          timeText.text = String(gameTime);
      }
  
      function timeExpired(e:TimerEvent):void
      {
          var gameTimer:Timer = e.target as Timer;
          gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
          gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
  
          interfaceLayer.addChild(gameEnd);
  
          var thisFont:Font = new myFont();
          var myFormat:TextFormat = new TextFormat();
          myFormat.font = thisFont.fontName;
  
          scoreText = new TextField();
          scoreText.defaultTextFormat = myFormat;
          scoreText.text = String(score);
          interfaceLayer.addChild(scoreText);
          scoreText.x = 278;
          scoreText.y = 180;
          scoreText.selectable = false;
          scoreText.embedFonts = true;
          scoreText.antiAliasType = AntiAliasType.ADVANCED;
          scoreText.setTextFormat(gameOverscoreFormat);
  
          Mouse.show();
          removeChild(gameLayer);
          addMenuListeners();
      }
  
      function moveCursor(event:Event) 
      {
        crosshair.x=mouseX;
        crosshair.y=mouseY;
      }
      }
    }
  </code></pre>
  
  <p>I'm not quite sure how to fix this, so any advice or solution will be welcome. I can't get it to work the way I intended without getting errors.</p>
  
  <p>Thanks.</p>