Search code examples
actionscript-3flashair

AS3 removeChild The supplied DisplayObject must be a child of the caller error


I've got a problem, here is the code I'm using

    public var monsterArray:Array;
    public var score:Number;

    public function MonsterManager()
    {
        score = 0;
        monsterArray = new Array();
    }

public function spawnMonster( waveNumber:Number ):void
    {
        //This code needs to choose the ID not the monster itself
        var id:Number;
        var meleeRanged = Math.floor(Math.random()*2)
        trace("meleeRanged :"+meleeRanged);
        id = Math.floor(Math.random()*3)+1;
        switch(id)
        {
            case 1: var newMonster1:Monster1 = new Monster1();
                        newMonster1.monsterSetup( waveNumber, id );
                        monsterArray.push( newMonster1 );
                        addChild( newMonster1 );
                break;
        }

    }

public function update(  ):void
    {
        //trace("Go To Update");
        var i:number;
        i = 0;
        for each ( var monster:Monster in monsterArray )
        //monster needs a bool variable to say if it is active, if it is dont reuse,
        //if it is not activate, we can use setup and re use it,
        {
            monster.update( );
            if(monster.hp <= 0)
            {
                score += 10;
                removeChild( monster );
                array.splice(i,1);
                //monster.x = -1000;
                //monster.hp = 1;
                i++;
            }
        }
    }

I have deleted the add text function, because that doesn't cause the problem :), but when i call apon the removeText function, i get this error in flash

The supplied DisplayObject must be a child of the caller.

The array is public, so I'm not sure why it can't be deleted, could someone show some light on this please.

Cheers.

New code below

  public function update( heroGra:HeroDisplay ):void
    {
        //trace("Go To Update");
        var count:Number = monsterArray.length;
        var monster:Monster;

        for( var i:int = 0; i<count; i++)
        {
            monster = monsterArray[i];
            monster.update( heroGra );


            if( monster.hp <= 0 )
            {
                score += 10;
                removeChild( monster );
                monsterArray.splice(i,1);
                i--;
            }
        }
    }

Canvas.

If there is more than 2 monsters in the monsterArray a error occurs, any idea on how i can get it to stop this?


Solution

  • Not sure if your for each function is doing a good job. Try to do it this way:

    public function update(  ):void
    {
        var count:Number = monsterArray.length;
        var monster:Monster;
    
        for( var i:int = 0; i<count; i++)
        {
            monster = monsterArray[i];
            monster.update( );
    
            if(monster.hp <= 0)
            {
                score += 10;
                removeChild( monster );
                array.splice(i,1);
                count--;
                i--;
            }
        }
    }