0

嗨,我有一个问题,我想我可能知道是什么原因造成的,但我不知道如何解决。我任何人都可以帮我解决这个问题,它会很棒......错误是

TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 Bullet/removeSelf()[C:\Users\Alan\Desktop\game copy\Bullet.as:56] 在 Bullet/loop()[C:\Users\Alan\Desktop\game copy\Bullet.as:44]

这是删除子弹 ps 的主要操作的代码。对不起帽子。

    stage.addEventListener(Event.ENTER_FRAME, testCollisions);

//Check for collisions between an enemies array and a Lasers array
function testCollisions(e:Event):void
{

    var tempEnemy:MovieClip;
    var tempLaser:MovieClip;

    for (var i:int=enemies.length-1; i >= 0; i--)
    {
        tempEnemy = enemies[i];
        for (var j:int=bullets.length-1; j>=0; j--)
        {
            tempLaser = bullets[j];
            if (tempLaser.hitTestObject(tempEnemy))
            {

                removeChild(tempEnemy);
                removeLaser(j);

            } 
        }
    }
}




function removeLaser(idx:int)
{
    parent.removeChild(bullets[idx]);
    bullets.splice(idx,1);
}

这是删除它的子弹类的代码

    public class Bullet extends MovieClip {

        private var speed:int = 30;
        private var initialX:int;
        public var eligableForRemove:Boolean = false;



        public function Bullet(playerX:int, playerY:int, playerDirection:String) {

            // constructor code
            if(playerDirection == "left") {
                speed = -30; //speed is faster if player is running
                x = playerX - 25;
            } else if(playerDirection == "right") {
                speed = 30;
                x = playerX + 25
            }
            y = playerY - 75;

            initialX = x; //use this to remember the initial spawn point

            addEventListener(Event.ENTER_FRAME, loop);
        }

        public function loop(e:Event):void
        {
            //looping code goes here
            x += speed;

            if(speed > 0) { //if player is facing right
                if(x > initialX + 640) { //and the bullet is more than 640px to the right of where it was spawned
                    removeSelf(); //remove it
                    eligableForRemove = true;
                }
            } else if (speed < 0) { //else if player is facing left
                if(x < initialX - 640) {  //and bullet is more than 640px to the left of where it was spawned
                    removeSelf(); //remove it
                    eligableForRemove = true;
                } else {
                    eligableForRemove = false;
                    }
            }
        }

        public function removeSelf():void
        {
            if(eligableForRemove == true){trace("remove self");
            removeEventListener(Event.ENTER_FRAME, loop); //stop the loop
            this.parent.removeChild(this); //tell this object's "parent object" to remove this object
            //in our case, the parent is the background because in the main code we said:
            //back.addChild(bullet);
            }

        }

    }

}

我认为是什么导致了它,当没有什么要删除时,它调用了一个空函数 removeSelf。所以我添加了 eligableForRemove 变量,但我可能没有正确放置它,所以如果有人能帮我解决这个问题,我将不胜感激......另外,如果我尝试从主要操作中删除项目符号,它会给我它必须是调用者错误的孩子。请帮忙。

4

1 回答 1

0

您是对的,这可能是由于尝试两次删除孩子而导致的,并且您在调用eligableForRemove 后进行removeSelf()了设置,因此removeSelf()看不到更新的值。

但是,我认为问题在于您正在移除子弹removeLaser(),但没有告诉子弹它已经死了,所以它继续运行循环,最终它超出了界限并尝试再次移除自己。

您应该能够摆脱eligableForRemove,只需更改removeLaser()为:

function removeLaser(idx:int)
{
    (bullets[idx] as Bullet).removeSelf();
    bullets.splice(idx,1);
}

(或者如果bullets是 aVector.<Bullet>你可以这样做bullets[idx].removeSelf(),因为运行时已经知道它是 a Bullet。)

于 2013-03-23T17:59:28.987 回答