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関数を呼び出すと、フラッシュでこのエラーが発生します

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

キャンバス。

モンスター配列に 2 つ以上のモンスターが存在する場合、エラーが発生します。これを止める方法はありますか?

4

1 に答える 1

0

for each 関数がうまく機能しているかどうかはわかりません。このようにしてみてください:

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 に答える