【问题标题】:Error #1009 In ActionScript 3 when I try to remove a bullet if it hits an enemy错误 #1009 在 ActionScript 3 中,当我尝试移除子弹击中敌人时
【发布时间】:2013-03-23 17:20:14
【问题描述】:

您好,我有一个问题,我想我可能知道是什么原因造成的,但我不知道如何解决。我任何人都可以帮我解决这个问题,那就太好了......错误是

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 变量,但我可能没有正确放置它,所以如果有人可以帮我解决这个问题,我将不胜感激......另外,如果我尝试从主要操作中删除项目符号,它会给我它必须是调用者错误的孩子。请帮忙。

【问题讨论】:

    标签: actionscript-3 flash actionscript game-development


    【解决方案1】:

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

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

    您应该可以摆脱eligableForRemove,只需将removeLaser() 更改为:

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

    (或者如果bulletsVector.&lt;Bullet&gt;,你可以直接使用bullets[idx].removeSelf(),因为运行时已经知道它是Bullet。)

    【讨论】:

    • 我刚要出发,但我会留意你明天是否有其他问题。
    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    相关资源
    最近更新 更多