0
votes

Hello I' am trying to make two collison walls in the form of two lines which I have given the instance name 'colission1' and 'colission2' I have used the following code to make the cars dissapear and the gameover message to pop up when they are git by the car;

addEventListener(Event.ENTER_FRAME, hit1);
function hit1(e:Event):void {

    if (car_mc.hitTestObject(colission2_mc)) 
    {
        gotoAndPlay(2);
        gotoAndStop(2);
        car_mc.visible = false;
        stop();
    }
    else
    {
        car_mc.visible = true;
    }
}

addEventListener(Event.ENTER_FRAME, hit2);
function hit2(e:Event):void {
    if (car_mc.hitTestObject(colission1_mc)) 
    {
        gotoAndPlay(2);
        gotoAndStop(2);
        car_mc.visible = false;
        stop();
    }
    else
    {
        car_mc.visible = true;
    }
}

and then I get this error;

TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Gamev1_fla::MainTimeline/hit1()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Gamev1_fla::MainTimeline/hit2()
2
you have given the instance 'colission1' and 'colission2' or 'colission1_mc' and 'colission2_mc' ?Zakaria Wahabi

2 Answers

0
votes

You are going to another frame where either car_mc or colission2_mc don't exist anymore but since you don't remove the enterframe listener it keeps running hit1 and you get an error. Remove the enterframe listener.

0
votes

Ideally you can do this on a single ENTER_FRAME event instead creating two event_frame events. Also when you can get rid of the error by one of following ways:

  1. When you move to next frame make sure the object should exist at that frame (it can be out of stage).
  2. Remove enter_frame event before you move to next frame.

Ideally thats how your code should be:

  addEventListener(Event.ENTER_FRAME, checkHits);
  function checkHits(e:Event):void {

if (car_mc.hitTestObject(colission2_mc)) 
{
    removeEventListener(Event.ENTER_FRAME, checkHits);
    gotoAndStop(2);
    car_mc.visible = false;
    stop();
}
else if (car_mc.hitTestObject(colission1_mc)) 
{
    removeEventListener(Event.ENTER_FRAME, checkHits);
    gotoAndStop(2);
    car_mc.visible = false;
    stop();
}
else
{
    car_mc.visible = true;
}


  }