AS3 removeChild提供的DisplayObject必须是调用方错误的子级

时间:2013-01-07 18:00:44

标签: actionscript-3 flash air

我遇到了问题,这是我正在使用的代码

    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++;
            }
        }
    }

我删除了添加文本功能,因为这不会导致问题:),但是当我调用apon的removeText函数时,我在flash中出现此错误

The supplied DisplayObject must be a child of the caller.

阵列是公开的,所以我不确定为什么它不能被删除,有人可能会对此有所了解。

干杯。

以下新代码

  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--;
            }
        }
    }

画布。

如果monsterArray中有超过2个怪物出现错误,那么我是如何知道如何阻止它的呢?

1 个答案:

答案 0 :(得分:0)

不确定您的每个功能是否都做得很好。尝试这样做:

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--;
        }
    }
}
相关问题