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 zombie shooter game where I get an error when I try to hit test two objects created with actionscript. Could someone post a possible fix please.

Here is the Error Code;

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

and here is my game code;

import flash.events.Event;
var zombiehits:Number = 2;
var numzombies1:Number = GenerateRandomNumber(10);
var zombie : Z = new Z();
var objz;
var objb;
stop();
//Change mouse
crosshair_mc.mouseEnabled = false;
crosshair_mc.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
Mouse.hide();
//EventListners;
stage.addEventListener(Event.ENTER_FRAME, alwayson);
stage.addEventListener(MouseEvent.MOUSE_MOVE,facecursor);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
//Code;
var startcountdown:Number = 5;
var fl_CountDownTimerInstance:Timer = new Timer(1000,startcountdown);
fl_CountDownTimerInstance.addEventListener(TimerEvent.TIMER_COMPLETE, startgame);
fl_CountDownTimerInstance.start();

function createZombie():void
{
    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 zombie = event.currentTarget;
    objz = zombie;
    zombie.y +=  1;
    if (zombie.hitTestObject(objb) == true)
    {
        dead -=  1;
    }
    if (dead == 0)
    {
        removeChild(zombie);
        zombie.removeEventListener(Event.ENTER_FRAME,zombiemove);
    }

}

function alwayson(event:Event)
{
    if (character_mc.hitTestObject(wallleft) == true)
    {
        character_mc.x +=  speedchar;
    }
    if (character_mc.hitTestObject(wallright) == true)
    {
        character_mc.x -=  speedchar;
    }
    if (character_mc.hitTestObject(walltop) == true)
    {
        character_mc.y +=  speedchar;
    }
    if (character_mc.hitTestObject(wallbottom) == true)
    {
        character_mc.y -=  speedchar;
    }
}

function fl_CustomMouseCursor(event:Event)
{
    crosshair_mc.x = stage.mouseX;
    crosshair_mc.y = stage.mouseY;
}

function facecursor(event):void
{
    character_mc.rotation = (180 * Math.atan2(mouseY - character_mc.y,mouseX - character_mc.x))/Math.PI;

}

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);
    }
}
character_mc.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)
{
    if (upPressed)
    {
        character_mc.y -=  speedchar;
    }
    if (downPressed)
    {
        character_mc.y +=  speedchar;
    }
    if (leftPressed)
    {
        character_mc.x -=  speedchar;
    }
    if (rightPressed)
    {
        character_mc.x +=  speedchar;
    }
}

function fl_SetKeyPressed(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.UP :
            {
                upPressed = true;
                break;



            };
        case Keyboard.DOWN :
            {
                downPressed = true;
                break;



            };
        case Keyboard.LEFT :
            {
                leftPressed = true;
                break;



            };
        case Keyboard.RIGHT :
            {
                rightPressed = true;
                break;



        }
    }
};

function fl_UnsetKeyPressed(event:KeyboardEvent):void
{

    switch (event.keyCode)
    {
        case Keyboard.UP :
            {
                upPressed = false;
                break;



            };
        case Keyboard.DOWN :
            {
                downPressed = false;
                break;



            };
        case Keyboard.LEFT :
            {
                leftPressed = false;
                break;



            };
        case Keyboard.RIGHT :
            {
                rightPressed = false;
                break;



        }
    }
};


function GenerateRandomNumber(limit:Number):Number
{
    var randomNumber:Number = Math.floor(Math.random() * (limit + 1));
    return randomNumber;
}

function startgame(event:Event)
{
    createZombie();
    trace("complete");
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

objb is likely null because you haven't fired a bullet yet, but your zombiemove function is testing with that variable.

try this :

function zombiemove(event:Event)
{
    var zombie = event.currentTarget;
    objz = zombie;
    zombie.y +=  1;
    if (objb != null)
    {
         if (zombie.hitTestObject(objb) == true)
         {
              dead -=  1;
         }
    }
    if (dead == 0)
    {
        removeChild(zombie);
        zombie.removeEventListener(Event.ENTER_FRAME,zombiemove);
    }

}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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