【问题标题】:AS3 removeChild The supplied DisplayObject must be a child of the caller errorAS3 removeChild 提供的 DisplayObject 必须是调用者错误的子对象
【发布时间】:2023-03-13 15:02:01
【问题描述】:

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

    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 个怪物,则会发生错误,我知道如何让它停止这种情况吗?

【问题讨论】:

  • removeChild() 用于从舞台上移除一些东西(即:屏幕上你可以看到的东西)。您不能删除以前从未添加过的内容(您可以使用addChild() 添加内容)。您打算从阵列或舞台上移除一些东西吗?你可能想展示更多你的代码:)

标签: actionscript-3 flash air


【解决方案1】:

不确定你的 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--;
        }
    }
}

【讨论】:

  • 是的,有一个叫做addText的方法,这会在数组中添加一些文本,我更新了我的问题
  • 我很抱歉我修改了上面的内容:D
  • 您不能将子字符串添加到 DisplayList,因为它不是 DisplayObject。改为创建 TextField...
  • 那应该更好,这段代码取自另一篇作品
  • 我会在升技尝试,需要自己做一些食物:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2013-04-18
相关资源
最近更新 更多