title: AS3 - Error #2025: The supplied DisplayObject must be a child of the caller
body: <p>After 2 days of trying and searching for an answer I still didn't found it. I keep getting Error #2025: The supplied DisplayObject must be a child of the caller. I'm making a game where if the user hits an enemy, the enemy get destroyed. The code:</p>
  
  <p>My main class</p>
  
  <pre><code>package classes
  {   
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.utils.Timer;
  
  public class Main extends MovieClip
  {
      var enemyTimer:Timer;
  
      public function Main() 
      {
          var user:ship = new ship();
          addChild(user);
          user.name = "user";
          user.initialize();
  
          enemyTimer = new Timer(2000);
          enemyTimer.addEventListener("timer", sendEnemy);
          enemyTimer.start();
  
          function sendEnemy(e:Event)
          {
              var badboy:enemy = new enemy();
              addChild(badboy);
              badboy.initialize();
          }
      }
  }
  }
  </code></pre>
  
  <p>the enemy class</p>
  
  <pre><code>package classes.enemy
  {   
  import flash.display.MovieClip;
  import flash.events.Event;
  
  public class Enemy extends MovieClip
  {
      var speed:Number;
  
      public function initialize()
      {
          addEventListener("enterFrame", enterFrame);
      }
  
      public function Enemy() 
      {
          this.x = 700;
          this.y = Math.random()*200 + 50;
          speed = Math.random()*5 + 5;
      }
  
      function enterFrame(e:Event)
      {
          this.x -= speed;
  
          if(this.hitTestObject(parent.getChildByName("user")))
          {
              kill();
          }
      }
  
      function kill()
      {
          removeEventListener("enterFrame", enterFrame);
          stage.removeChild(this);
      }
  }
  }
  </code></pre>
  
  <p>The files are in different folders (classes > Main.as &amp; classes.enemy.Enemy.as), don't know if that has anything to do with it.</p>
  
  <p>Any help would be appreciated.</p>