0

我有问题,这是我正在使用的代码

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

我已经删除了添加文本功能,因为这不会导致问题:),但是当我调用 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 个怪物,则会发生错误,我知道如何让它停止这种情况吗?

4

1 回答 1

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--;
        }
    }
}
于 2013-01-07T18:06:16.253 回答