title: Getting error when using the removeChild method
body: <p>Happy Pre-Halloween everyone :)</p>
  
  <p>My issue today is a DisplayObject error I'm getting when remove a child object. I have code that will launch(addChild) a video container and video controls as well as add a close button. <a href="http://gamerfeed.tv/stackoverflow/button_overlap/displayobject.html" rel="nofollow noreferrer">Now the close button works fine and everything</a>, removing the video and controls and I'm able to choose another video again, but when you click close a 2nd time I get this error:</p>
  
  <p><strong>ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild()</strong></p>
  
  <p>So I've narrowed down the problem to where I remove the videoContainer (which holds the video object)</p>
  
  <p><strong>My code to play the videos:</strong></p>
  
  <pre><code>public function videoSwitch(videoName):void
  {
      nv.closeOut();
      nv.resetNav = false;
  
      if (!videoPlaying)
      {
          vc = new VideoClass(videoName, videoHolder);
          vc.addEventListener("KillMovie", removePlayer);
          container.addChild(videoContainer);
          container.addChild(vc);
          //container.addChildAt(videoContainer, 1);
          //container.addChildAt(vc, 2);
          videoPlaying = true;
          closeVideo();
      }
  
      else if (videoPlaying)
      {
          vc.clearSource();
          container.removeChild(videoContainer);
          container.removeChild(vc);
  
          vc = new VideoClass(videoName, videoHolder);
          vc.addEventListener("KillMovie", removePlayer);
          container.addChild(videoContainer);
          container.addChild(vc);
          //container.addChildAt(videoContainer, 1);
          //container.addChildAt(vc, 2);
          closeVideo();
      }
          trace("videoPlaying = "+videoPlaying+"\r");
  }
  </code></pre>
  
  <p><strong>The close out video player code:</strong>
  You can see in my comments other code I tried, but still getting the error.</p>
  
  <pre><code>function closeVideo():void 
  {
      closeBtn.visible = true;
      closeBtn.x = 770;
      closeBtn.y = 20;
      closeBtn.buttonMode = true;
      container.addChild(closeBtn);
      closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
  
      function closeButtonClicked(event:MouseEvent):void 
      {
          vc.clearSource();
          container.removeChild(videoContainer);
          //container.removeChildAt(videoContainer, 1);
          container.removeChild(vc);
          videoPlaying = false;
          closeBtn.visible = false;
      }
  }
  </code></pre>
  
  <p>Now <a href="http://gamerfeed.tv/stackoverflow/button_overlap/displayobject.html" rel="nofollow noreferrer">my movie</a> works fine, but I'm worried that this error happening in the background (and showing up in my output window) will eventually cause a problem else where :(</p>
  
  <p>Thanks in advance for any eyes on this one! :)</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>
  <strong>FIXED!</strong> The problem was I remove the kill VC listener, but forgot to remove the stupid Close Button Mouse_Event listener :(</p>
  
  <pre><code>function addCloseButton():void 
  {
      container.addChild(closeBtn);
      closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
  
      function closeButtonClicked(event:MouseEvent):void 
      {
          videoPlaying=false;
          vc.clearSource();
          removeContainerChildren(); // &lt;- thx Joel!
          closeBtn.removeEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
          //^ Forgot this line - thx Jotham!
          container.removeChild(closeBtn);
      }
  }
  </code></pre>
  
  <p>Don't know if this graphic helps but:
  <img src="https://farm3.static.flickr.com/2638/4058417529_11928bdd00_o.jpg" alt="alt text"></p>