Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am making a game where i want to be able to shoot zombies to make their health go down, but i get a null property error on objb.

i have declared abjb and objz as Object variables

Here is my code for the bullet:

function mouseDown(event)
{
    var b = new Bullet();
    b.x = character_mc.x;
    b.y = character_mc.y;
    b.angleRadian = Math.atan2(mouseY - character_mc.y,mouseX -character_mc.x);
    b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    addChild(b);
}



function bulletEnterFrame(event)
{
    var b = event.currentTarget;
    objb = event.currentTarget;
    b.x +=  Math.cos(b.angleRadian) * speedb;
    b.y +=  Math.sin(b.angleRadian) * speedb;
    b.rotation = b.angleRadian * 180 / Math.PI;
    if (b.hitTestObject(objz) == true)
    {
        removeChild(b);
        b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    }
    if (b.x < 50 || b.x > 675 || b.y < 50 || b.y > 500)
    {
        removeChild(b);
        b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    }
}

and here is my code from the zombie:

function createZombie():void
{
    var zombie : Z = new Z();
    zombie.rotation = 90;
    zombie.height = 85.35;
    zombie.width = 55.9;
    zombie.x = 403.25;
    zombie.y = -86.9;
    addChild(zombie);
    trace("spawned the zombie");
    zombie.addEventListener(Event.ENTER_FRAME,zombiemove);
}

function zombiemove(event:Event)
{
    var dead:Number;
    var zombie = event.currentTarget;
    objz = zombie;
    dead = zombiehits;
    zombie.y +=  1;
    if (zombie.hitTestObject(objb) == true)//this is where the error is
    {
        dead -= 1;
    }
    if (dead == 0)
    {
        removeChild(zombie);
        zombie.removeEventListener(Event.ENTER_FRAME,zombiemove);
    }

}
share|improve this question

closed as off topic by Byte56, Sean Middleditch, bummzack, Trevor Powell, Josh Petrie Mar 22 '13 at 15:19

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

Your code is quite hard to read, I'm a bit puzzled by objb being a single global as that would imply that there can only ever be one bullet at a time.

Your use of Event.ENTER_FRAME seems to be one big race condition. It seems pretty clear that it matters in what order the functions are called, but with functions constantly being attached and removed from the event it's hard to grasp exactly what will happen.

In any case, if the player hasn't fired a bullet, what would the value of objb be?

Numerous changes could be made, but I'd suggest the following:

  • Attach just one frame handler function to Event.ENTER_FRAME and have that function take care of everything that has to happen in a frame.
  • Store all of your zombie objects in a list, store all of your bullet objects in a list. Iterate over these lists as necessary in your frame handler function.

With just one event handler you have got complete control of the handling order.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.