【问题标题】:ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.ArgumentError:错误 #2025:提供的 DisplayObject 必须是调用者的子对象。
【发布时间】:2013-09-07 00:15:07
【问题描述】:

拜托,我需要帮助,试图从我的舞台上移除子弹和敌人。我对编程很陌生。谢谢。

我收到以下错误消息:

ArgumentError:错误 #2025:提供的 DisplayObject 必须是子对象 来电者的。在 flash.display::DisplayObjectContainer/removeChild() 在 Main/fl_EnterFrameHandler() 在 flash.utils::Timer/_timerDispatch() 在 flash.utils::Timer/tick()

debug 发给我的代码。

package 
{

import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;

public class Main extends MovieClip
{

    var enemyShipTimer:Timer;
    var coinTimer:Timer;
    var playerscore:Number = 0;
    var enemies:Array;
    var bullets:Array;

    public function Main()
    {
        enemyShipTimer = new Timer(1000);
        enemyShipTimer.addEventListener("timer", fl_EnterFrameHandler);
        enemyShipTimer.start();
        coinTimer = new Timer(1000);
        coinTimer.start();
        enemies = new Array ();
        bullets = new Array ();

    }

    function fl_EnterFrameHandler(event:Event):void
    {
        var enemyinstance = new enemy_ks();
        stage.addChild(enemyinstance);
        enemies.push(enemyinstance);
        trace(enemies.length);

        for (var count=0; count<enemies.length; count++)
        {

            for (var bcount=0; bcount<bullets.length; bcount++)
            {
                if (enemies[count].hitTestObject(bullets[bcount]))
                {
                    removeChild(enemies[count]);
                    enemies.splice(count, 1);
                    removeChild(bullets[bcount]);
                    bullets.splice(bcount, 1);
                }
            }
            score_ks.text = " " + playerscore;
        }


    }

}
}

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    编辑:重新阅读您的代码并注意到真正的错误实际上是您正在添加到舞台但从主精灵中删除。你需要匹配这些。如果对象实际上不是该对象的子对象,则您不能从父对象中移除该对象。

    以下几点仍然需要解决,否则您可能会遇到其他错误。


    您的问题在于您的循环。在您的循环中,您在每次成功的命中测试中调整数组长度,但您从不调整循环的计数。

    这样想吧。

    你开始:

    count = 0;
    length = 10;
    

    现在假设您为count &lt; length 运行一个循环,然后在count == 4count == 7 处拼接。在您当前的方案中,您只会命中以下对象(使用原始索引)

    0 1 2 3 4 6 7 9
    

    请注意,您没有达到索引 5 或 8。当您像这样修改数组并且不修改计数时,您最终会跳过某些项目。拼接索引 4 后,原始索引 5 移动到 4,其他所有内容也向后移动 1。所以当你移动到索引 5 时,你实际上是在读取原始索引 6。

    对此非常简单的解决方法是在拼接时调整计数。

    if (enemies[count].hitTestObject(bullets[bcount]))
    {
        removeChild(enemies[count]);
        enemies.splice(count, 1);
        count--; //subtract 1 from the count
        removeChild(bullets[bcount]);
        bullets.splice(bcount, 1);
        bcount--; //subtract 1 from the bcount
    }
    

    这将确保您的计数实际命中每个对象。您也可以使用for each 循环来处理这个问题,尽管这种类型的循环比标准 for 循环慢,具体取决于应用程序。

    此外,如果 DisplayObject 实际上是该容器的子容器,则只能从 DisplayObjectContainer 中删除它。如果不是,则会出错。因此,我相信您可能还会遇到一个问题,即您的数组与舞台上的内容不完全一致,并且您正在尝试删除一个实际上并不作为父项的子项存在的对象

    除了未成年人,您应该避免将孩子直接添加到舞台上,除非您有真正的理由这样做。而是使用this 关键字直接将其添加到应用程序对象的显示列表中,或者简单地使用addChild()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多