Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 ArgumentError:Error#2025:提供的DisplayObject必须是调用者的子对象。_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 ArgumentError:Error#2025:提供的DisplayObject必须是调用者的子对象。

Actionscript 3 ArgumentError:Error#2025:提供的DisplayObject必须是调用者的子对象。,actionscript-3,flash,Actionscript 3,Flash,求你了,我需要帮助把子弹和敌人从舞台上移开。我对编程很陌生。多谢各位 我收到以下错误消息: ArgumentError:Error#2025:提供的DisplayObject必须是子对象 打电话的人的名字。在flash.display::DisplayObjectContainer/removeChild()中 在Main/fl_EnterFrameHandler()处 flash.utils::Timer/_timerDispatch()在flash.utils::Timer/tick()处

求你了,我需要帮助把子弹和敌人从舞台上移开。我对编程很陌生。多谢各位

我收到以下错误消息:

ArgumentError:Error#2025:提供的DisplayObject必须是子对象 打电话的人的名字。在flash.display::DisplayObjectContainer/removeChild()中 在Main/fl_EnterFrameHandler()处 flash.utils::Timer/_timerDispatch()在flash.utils::Timer/tick()处

调试发送给我的代码

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


    }

}
}
包
{
导入flash.display.MovieClip;
导入flash.events.Event;
导入flash.utils.Timer;
公共类Main扩展了MovieClip
{
var-enemyShipTimer:计时器;
var-coinTimer:定时器;
var playerscore:Number=0;
变种敌人:阵列;
变量:数组;
公共功能Main()
{
enemyShipTimer=新计时器(1000);
enemyShipTimer.addEventListener(“计时器”,fl_EnterFrameHandler);
enemyShipTimer.start();
coinTimer=新计时器(1000);
coinTimer.start();
敌人=新阵列();
项目符号=新数组();
}
函数fl_EnterFrameHandler(事件:事件):void
{
var enemyinstance=新敌人_ks();
stage.addChild(enemyinstance);
敌人。推(enemyinstance);
痕迹(长度);

对于(var count=0;countEDIT):重新阅读您的代码,注意到真正的错误实际上是您正在添加到舞台,但正在从主精灵中删除。您需要将这些匹配起来。如果对象实际上不是父对象的子对象,则无法从父对象中删除该对象

下面的几点仍然需要解决,否则您可能会出现其他错误


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

所以就这样想吧

您可以从以下内容开始:

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

请注意,您没有点击索引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
}
这将确保您的计数实际命中每个对象。您也可以对每个
循环使用一个
,尽管该类型的循环比标准的循环慢,具体取决于应用程序

此外,只有当
DisplayObjectContainer
实际上是该容器的子容器时,才能从该容器中删除
DisplayObjectContainer
。如果不是,则会出错。因此,我相信您也可能会遇到这样一个问题,即您的数组与舞台上的数组不完全对齐,并且您正试图删除一个不匹配的对象oesn实际上不是作为父母的孩子存在的

除此之外,您应该避免将子对象直接添加到后台,除非您有真正的理由这样做。相反,使用
this
关键字直接将其添加到应用程序对象的显示列表中,或者只需
addChild()

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
}