Skip navigation
Currently Being Moderated

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

Jul 20, 2012 1:30 AM

I get this error and I think I understand it but I look at my code and the enemy does seem to be on the stage. The error is on the last line of the Enemy.as. It can't remove the enemy display object as it seems that the stage doesn't have it as a child.

 

Main.as (instantiates enemy:Enemy)

Enemy.as (holds the problem code) Basically it is saying that the stage doesn't hold the display object enemy.

I will post the parts where enemy is mentioned.

Main.as

private var enemy:Enemy = new Enemy(stage, hero);

...

for (var i:int=0; i< 5; i++)

                              {

                                        enemy = new Enemy(stage, hero);

                                        enemy.x = 100 + offsetEnemy;

                                        enemyVector[i] = enemy;//populate the vector with enemies.

                                        addChild(enemy);

                                        enemy.cacheAsBitmap = true;

                              }

 

Enemy.as

package

{

 

          import flash.display.MovieClip;

          import flash.display.Stage;

          import flash.events.Event;

          import flash.display.Stage;

 

          public class Enemy extends MovieClip

          {

                    private var stageRef:Stage;

                    private var vy:Number = 1; //y velocity

                    private var ay:Number = .1; //y acceleration

                    private var target:Hero;

 

                    public function Enemy(stageRef:Stage, target:Hero) : void

                    {

                              this.stageRef = stageRef;

                              this.target = target;

 

                              x = Math.random() * stageRef.stageWidth;

                              y = -5;

 

                              addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

                    }

                    private function loop(e:Event) : void

                    {

                              vy += ay;

                              y += vy;

 

                              if (y > stageRef.stageHeight)

                                        removeSelf();

                    }

 

                    private function removeSelf() : void {

 

                              removeEventListener(Event.ENTER_FRAME, loop);

 

                              if (stageRef.contains(this))

                                        stageRef.removeChild(this);

                    }

 

          }

 

}

 
Replies
  • Currently Being Moderated
    Jul 20, 2012 5:20 AM   in reply to codeBeastAdobe

    If I add the enemy in the main class, then I would remove the enemy in the main class.

     

    If you want the enemy to reference the stage, then you should use an ADDED_TO_STAGE event listener in the instantiation function and have its event handler deal with processing anything relative to the stage for that instance.

     

    Don't use the same names for different instances of variables.  You won't get anywhere assigning a variable to itself ('this' is most often unnecessary in AS3)...

     

    this.stageRef = stageRef;

    this.target = target;

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points