为什么我在定义的对象上执行`hitTest`时收到空错误?

时间:2015-06-17 09:13:40

标签: actionscript-3 flash flash-cs3

我试图做的就是从舞台上移除一些孩子,但它却给我一个空错误:

TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display::DisplayObject/hitTestObject()
at SpaceOddessey_fla::MainTimeline/GreenMove()

我不明白为什么会这样,因为我在“绿色家伙”的移动/移除功能上方的函数中定义了对象。我无法继续这个项目,直到我得到修复,因为我需要将它用于其余的精灵。

这是我的代码

stop();

//do this three times (for each space dude)
//GREEN
var MaxGreen:int = 1;
//Here we declare the speed of these cannon balls
var GreenVelocityX:Array = new Array();
var GreenVelocityY:Array = new Array();
//keep track of how many green guys
var GreenCount:int = 0;
var GreenSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theGreen:Green_Guy;
//create the array thats holds the green guys
var myGreenGuyArray:Array = new Array(); 



//YELLOW
var MaxYellow:int = 1;
//Here we declare the speed of these cannon balls
var YellowVelocityX:Array = new Array();
var YellowVelocityY:Array = new Array();
//keep track of how many yellow guys
var YellowCount:int = 0;
var YellowSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theYellow:Yellow_Guy;
//create the array thats holds the yellow guys
var myYellowGuyArray:Array = new Array();



//BABY
var MaxBabies:int = 1;
//Here we declare the speed of these cannon balls
var BabyVelocityX:Array = new Array();
var BabyVelocityY:Array = new Array();
//keep track of how many baby guys
var BabyCount:int = 0;
var BabySprite:Sprite;//declare a global variable for the cannonBall sprite.
var theBaby:Baby_Guy;
//create the array thats holds the baby guys
var myBabyGuyArray:Array = new Array();



//SPACE SHIP
var MaxShips:int = 4;
//Here we declare the speed of these cannon balls
var ShipVelocityX:Array = new Array();
var ShipVelocityY:Array = new Array();
//keep track of how many baby guys
var ShipCount:int = 0;
var ShipSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theShip:space_ship;
//create the array thats holds the baby guys
var ShipArray:Array = new Array();


//ASTEROID
var MaxRocks:int = 6;
//Here we declare the speed of these cannon balls
var RockVelocityX:Array = new Array();
var RockVelocityY:Array = new Array();
//keep track of how many baby guys
var RockCount:int = 0;
var RockSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theRock:rock;
//create the array thats holds the baby guys
var RockArray:Array = new Array();

yellow_btn.addEventListener(MouseEvent.CLICK, ClickHandle);
green_btn.addEventListener(MouseEvent.CLICK, ClickHandle);
baby_btn.addEventListener(MouseEvent.CLICK, ClickHandle);
function ClickHandle(targetEvent:MouseEvent):void
{
    if(targetEvent.target == yellow_btn)
    {
        MaxYellow += 1;

        stage.addEventListener(Event.ENTER_FRAME,makeYellow);
    }
    else if(targetEvent.target == green_btn)
    {
        MaxGreen += 1;
        stage.addEventListener(Event.ENTER_FRAME,makeGreen);
    }
    else if(targetEvent.target == baby_btn)
    {
        MaxBabies += 1;
        stage.addEventListener(Event.ENTER_FRAME,makeBaby);
    }
}

stage.addEventListener(Event.ENTER_FRAME,makeShips);

function makeShips(Space:Event){
    //Create the Enemies if there are less then 100
    if (ShipCount < MaxShips){
        //create our sprites
        ShipSprite = new Sprite();
        stage.addChild(ShipSprite);
        theShip = new space_ship();
        ShipSprite.addChild(theShip);

        ShipSprite.x = Math.random() * 500;//width ~ 81.7
        ShipSprite.y = Math.random() * 300;

        //allows a scaler of making objects from big to small
        //BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;

        ShipArray[ShipCount] = ShipSprite;
        ShipCount += 1;

        stage.addEventListener(Event.ENTER_FRAME,ShipMove);
        }

        //end for loop
    }
function ShipMove(go:Event):void
{
    for(var s=0; s<ShipCount; s++)
    {
        ShipVelocityX[s] = -3;
        ShipVelocityY[s] = -1;
        /*if(my)
        {

        }*/
        ShipArray[s].x += ShipVelocityX[s];
        //myYellowGuyArray[a].y += YellowVelocityY[a];
        if (ShipArray[s].x > 550)
        {
            ShipArray[s].x = 0 - ShipArray[s].width;
        }
        else if(ShipArray[s].x + ShipArray[s].width < 0)
        {
            ShipArray[s].x = 500;
        }
        if(ShipArray[s].y > 400)
        {
            ShipArray[s].y = 0 - ShipArray[s].height;
        }
        else if(ShipArray[s].y + ShipArray[s].height < 0)
        {
            ShipArray[s].y = 400;
        }
    }
}

stage.addEventListener(Event.ENTER_FRAME,makeBaby);

function makeBaby(DudeEvent:Event){
    //Create the Enemies if there are less then 100
    if (BabyCount < MaxBabies){
        //create our sprites
        BabySprite = new Sprite();
        stage.addChild(BabySprite);
        theBaby = new Baby_Guy();
        BabySprite.addChild(theBaby);

        BabySprite.x = Math.random() * 500;//width ~ 81.7
        BabySprite.y = Math.random() * 300;

        //allows a scaler of making objects from big to small
        //BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;

        myBabyGuyArray[BabyCount] = BabySprite;
        BabyCount += 1;
        //trace(myEnemies.length);

        stage.addEventListener(Event.ENTER_FRAME,BabyMove);
        }

        //end for loop
    }
function BabyMove(go:Event):void
{
    for(var i=0; i<BabyCount; i++)
    {
        BabyVelocityX[i] = 5;
        BabyVelocityY[i] = 2;
        /*if(my)
        {

        }*/
        myBabyGuyArray[i].x += BabyVelocityX[i];
        myBabyGuyArray[i].y += BabyVelocityY[i];
        myBabyGuyArray[i].rotation += 2;
        if (myBabyGuyArray[i].x > 550)
        {
            myBabyGuyArray[i].x = 0 - myBabyGuyArray[i].width;
        }
        else if(myBabyGuyArray[i].x + myBabyGuyArray[i].width < 0)
        {
            myBabyGuyArray[i].x = 500;
        }
        if(myBabyGuyArray[i].y > 400)
        {
            myBabyGuyArray[i].y = 0 - myBabyGuyArray[i].height;
        }
        else if(myBabyGuyArray[i].y + myBabyGuyArray[i].height < 0)
        {
            myBabyGuyArray[i].y = 400;
        }
        for(var v:int=0; v<RockCount; v++)
        {
            if(RockArray[v].hitTestObject(myBabyGuyArray[i]))
            {
                //trace('hey')
                BabyVelocityX[i] = 100;
            }
        }
    }
}

stage.addEventListener(Event.ENTER_FRAME,makeYellow);

function makeYellow(Dude:Event){
    //Create the Enemies if there are less then 100
    if (YellowCount < MaxYellow){
        //create our sprites
        YellowSprite = new Sprite();
        stage.addChild(YellowSprite);
        theYellow = new Yellow_Guy();
        YellowSprite.addChild(theYellow);

        YellowSprite.x = Math.random() * 500;//width ~ 81.7
        YellowSprite.y = Math.random() * 300;

        //allows a scaler of making objects from big to small
        //BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;

        myYellowGuyArray[YellowCount] = YellowSprite;
        YellowCount += 1;

        stage.addEventListener(Event.ENTER_FRAME,YellowMove);
        }

        //end for loop
    }
function YellowMove(go:Event):void
{
    for(var a=0; a<YellowCount; a++)
    {
        YellowVelocityX[a] = -3;
        YellowVelocityY[a] = -1;
        /*if(my)
        {

        }*/
        myYellowGuyArray[a].x += YellowVelocityX[a];
        myYellowGuyArray[a].y += YellowVelocityY[a];
        myYellowGuyArray[a].rotation += 1;
        if (myYellowGuyArray[a].x > 550)
        {
            myYellowGuyArray[a].x = 0 - myYellowGuyArray[a].width;
        }
        else if(myYellowGuyArray[a].x + myYellowGuyArray[a].width < 0)
        {
            myYellowGuyArray[a].x = 500;
        }
        if(myYellowGuyArray[a].y > 400)
        {
            myYellowGuyArray[a].y = 0 - myYellowGuyArray[a].height;
        }
        else if(myYellowGuyArray[a].y + myYellowGuyArray[a].height < 0)
        {
            myYellowGuyArray[a].y = 400;
        }
        for(var u:int=0; u<RockCount; u++)
        {
            if(RockArray[u].hitTestObject(myYellowGuyArray[a]))
            {
                YellowVelocityX[a] *= -1;
            }
        }
    }
}

stage.addEventListener(Event.ENTER_FRAME,makeGreen);

function makeGreen(Evnt:Event){
    //Create the Enemies if there are less then 100
    if (GreenCount < MaxGreen){
        //create our sprites
        GreenSprite = new Sprite();
        stage.addChild(GreenSprite);
        theGreen = new Green_Guy();
        GreenSprite.addChild(theGreen);

        GreenSprite.x = Math.random() * 500;//width ~ 81.7
        GreenSprite.y = Math.random() * 300;

        //allows a scaler of making objects from big to small
        //BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;

        myGreenGuyArray[GreenCount] = GreenSprite;
        GreenCount += 1;

        stage.addEventListener(Event.ENTER_FRAME,GreenMove);
        }
        //end for loop
    }
function GreenMove(go:Event):void
{
    for(var b:int=0; b<GreenCount; b++)
    {
        GreenVelocityX[b] = 7;
        GreenVelocityY[b] = -4;

        myGreenGuyArray[b].x += GreenVelocityX[b];
        myGreenGuyArray[b].y += GreenVelocityY[b];
        myGreenGuyArray[b].rotation -= 1;
        if (myGreenGuyArray[b].x > 550)
        {
            myGreenGuyArray[b].x = 0 - myGreenGuyArray[b].width;
        }
        else if(myGreenGuyArray[b].x + myGreenGuyArray[b].width < 0)
        {
            myGreenGuyArray[b].x = 500;
        }
        if(myGreenGuyArray[b].y > 400)
        {
            myGreenGuyArray[b].y = 0 - myGreenGuyArray[b].height;
        }
        else if(myGreenGuyArray[b].y + myGreenGuyArray[b].height < 0)
        {
            myGreenGuyArray[b].y = 400;
        }
        for(var z:int=0; z<ShipCount; z++)
        {
            //if(myGreenGuyArray[b] == null)
            {
                if(ShipArray[z].hitTestObject(myGreenGuyArray[b]))
                {
                    //if(GreenCount > 1)
                    //{
                        myGreenGuyArray[b].y = -50;
                        stage.removeChild(myGreenGuyArray[b]);
                        myGreenGuyArray.splice(b,1);
                        GreenCount--;
                        //b--;
                    //}
                }
            }
        }
        for(var g:int=0; g<RockCount; g++)
        {
            if(RockArray[g].hitTestObject(myGreenGuyArray[b]))
            {
                GreenVelocityX[b] *= -1;
            }
        }
    }
}

stage.addEventListener(Event.ENTER_FRAME,makeRocks);

function makeRocks(RockHard:Event){
    //Create the Enemies if there are less then 100
    if (RockCount < MaxRocks){
        //create our sprites
        RockSprite = new Sprite();
        stage.addChild(RockSprite);
        theRock = new rock();
        RockSprite.addChild(theRock);

        RockSprite.x = Math.random() * 500;//width ~ 81.7
        RockSprite.y = Math.random() * 300;

        //allows a scaler of making objects from big to small
        //BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;

        RockArray[RockCount] = RockSprite;
        RockCount += 1;

        stage.addEventListener(Event.ENTER_FRAME,RockMove);
        }

        //end for loop
    }
function RockMove(go:Event):void
{
    for(var r=0; r<RockCount; r++)
    {
        RockVelocityX[r] = 7;
        RockVelocityY[r] = 3;
        /*if(my)
        {

        }*/
        RockArray[r].x += RockVelocityX[r];
        //RockArray[r].y += RockVelocityY[r];
        if (RockArray[r].x > 550)
        {
            RockArray[r].x = 0 - RockArray[r].width;
        }
        else if(RockArray[r].x + RockArray[r].width < 0)
        {
            RockArray[r].x = 500;
        }
        if(RockArray[r].y > 400)
        {
            RockArray[r].y = 0 - RockArray[r].height;
        }
        else if(RockArray[r].y + RockArray[r].height < 0)
        {
            RockArray[r].y = 400;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须重新构建并简化代码。您必须不惜一切代价消除嵌套的enter_frame事件。 理想情况下,您可以使用一个ENTER_FRAME事件完成所有事情。

执行以下最接近您的代码且更清洁的内容

  1. 为make和move创建函数
  2. 通过for循环调用make函数(而不是通过enter_frame)
  3. 一旦调用了所有make函数,就创建一个enter_frame事件并调用其中的move函数。
  4. 然而,你可以进一步使它更清洁,但至少做到这一点将使你开始实现你的目标。

答案 1 :(得分:0)

  

我不明白为什么会发生这种情况

这听起来微不足道,但由于错误描述中给出的原因,这种情况正在发生:

  

参数hitTestObject必须为非null。

这是人们必须接受的事实。没有其他办法了。没有“但我确信这不应该发生”这样的事情。通过提出更好的论点,这不像是可以赢得的讨论。从某种意义上说,编译器总是正确的。你无法反驳它。

  

我在“绿色家伙”的移动/移除功能上方的函数中定义了对象

没有。错误说你没有,所以你没有。参数为null。否认事实毫无意义。

真正的问题不是“为什么它说它是空的,即使它不是?”,但“为什么值为null?

错误表明它出现在hitTestObject()函数中。堆栈跟踪解释它发生在GreenMove()函数中。有两条线可能导致这种情况。

if(ShipArray[z].hitTestObject(myGreenGuyArray[b]))

if(RockArray[g].hitTestObject(myGreenGuyArray[b]))

您应该在每一行放置断点,再次使用调试器运行代码并尝试重现错误。这是调试代码的方式。你不会看五个小时,希望这个原因会让你感到沮丧,因为你找不到它。

发生错误时,hitTestObject会认为myGreenGuyArray[b]null。从这一点来看,你知道b会发生什么价值。再次运行它。让它再次失败。看看它是否始终以b的相同值发生。

这一行看起来很可疑,因为它会改变数组。

myGreenGuyArray.splice(b,1);

您的问题是由于数组的混乱使用造成的。想想看:如果只有一个元素被删除了怎么办?如果再次访问该数组的(已删除,不存在)元素会发生什么?

trace([1,2,3][37] == null); // does the result of accessing an element that doesn't exist equal null?

解决这个问题

  1. 摆脱不必要的计数变量。数组有一个长度 正是出于这个目的的财产。
  2. 向后遍历数组。这样可以防止跳过 如果一个元素被移除并且不需要奇怪的减计数来解释它,它就可以正常工作。
  3. 删除元素后,请停止循环并转到下一个元素 continue和/或break的迭代,可能是组合 使用label,跳出嵌套循环。