Вопрос

Hi I have a problem and I think i might know what is causing it but I dont know how to solve it. I anyone can help me solve this problem it would be great...The error is

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/removeSelf()[C:\Users\Alan\Desktop\game copy\Bullet.as:56] at Bullet/loop()[C:\Users\Alan\Desktop\game copy\Bullet.as:44]

HERE IS THE CODE FOR MAIN ACTIONS THE ONE THAT REMOVES THE BULLET ps. sorry for caps.

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

HERE IS THE CODE FOR THE BULLET CLASS WHERE IT REMOVES IT

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

        }

    }

}

I think what is causing it, that it is calling a empty function removeSelf when there is nothing to remove. so I added the eligableForRemove variable but I might not have placed it correctly so if anyone can please help me solve this problem I would appreciate it...Also if I try the remove the bullet from the main Actions it give me the it must be a child of the caller error. Please Help.

Это было полезно?

Решение

You're right that this can be caused by trying to remove a child twice, and you're setting eligableForRemove after you call removeSelf(), so removeSelf() doesn't see the updated value.

However, I think the problem is that you're removing the bullet in removeLaser(), but not telling the bullet that it has died, so it keeps running the loop, and eventually it goes out of bounds and tries to remove itself again.

You should be able to get rid of eligableForRemove, and just change removeLaser() to:

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

(Or if bullets is a Vector.<Bullet> you can just do bullets[idx].removeSelf(), as the runtime already knows it's a Bullet.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top