Skip to main content
Inspiring
January 1, 2013
Answered

Parameter hitTestObject must be non-null.

  • January 1, 2013
  • 2 replies
  • 2242 views

I am creating a game wherein there are 4 buttons. The 4 buttons are baskets with labels of solid, liquid, gas and fire. When the button is clicked, the basket that the catcher/character is holding will change depending on what the button is clicked. The changing of the basket  is okay now. I placed the changing of basket animation inside the character/catcher movieClip and what I do is that when I clicked the desired basket, I will call the gotoAndPlay function. Now, my problem is regarding my hitTestObject. I want the solid objects to be catching the solid objects only and so on. But whenever I clicked on the button, the basket will change but the catcher/character won't move because of this error. What should I do? >.<

Here is my code in hitTestObject:

public function moveObjects(e:Event) {

for (var i: int = objects.length -1; i >= 0; i--) {

objects.y += speed;

addChild(objects);

if (objects.hitTestObject(catcher.mouth_mc_2)) {

      if (objects.typestr == "solid") {

      score += 5;

      soundfx_1.play();

      catcher.mouth_mc_2.gotoAndPlay(9);

      }

     else

     {

     score -= 5;

     soundfx_2.play();

     catcher.mouth_mc_2.gotoAndPlay(2);

     }

     if (score < 0) score = 0;

    ScoreDisplay.text = String(score);

    removeChild(objects);

    objects.splice(i, 1);

    }

   else if (objects.hitTestObject(catcher.mouth_mc_3)) {

         if (objects.typestr == "liquid") {

         score += 5;

         soundfx_1.play();

         catcher.mouth_mc_3.gotoAndPlay(9);

         }

       else

        {

        score -= 5;

        soundfx_2.play();

        catcher.mouth_mc_3.gotoAndPlay(2);

        }

       if (score < 0) score = 0;

      ScoreDisplay.text = String(score);

      removeChild(objects);

      objects.splice(i, 1);

      }

     else if (objects.hitTestObject(catcher.mouth_mc_4)) {

            if (objects.typestr == "gas") {

            score += 5;

            soundfx_1.play();

            catcher.mouth_mc_4.gotoAndPlay(9);

            }

            else

            {

            score -= 5;

            soundfx_2.play();

            catcher.mouth_mc_4.gotoAndPlay(2);

            }

            if (score < 0) score = 0;

           ScoreDisplay.text = String(score);

           removeChild(objects);

           objects.splice(i, 1);

           }

           else if (objects.hitTestObject(catcher.mouth_mc_5)) {

            if (objects.typestr == "fire") {

            score += 5;

            soundfx_1.play();

            catcher.mouth_mc_5.gotoAndPlay(9);

            }

            else

            {

            score -= 5;

            soundfx_2.play();

            catcher.mouth_mc_5.gotoAndPlay(2);

            }

            if (score < 0) score = 0;

            ScoreDisplay.text = String(score);

            removeChild(objects);

            objects.splice(i, 1);

            }

    else if (objects.hitTestObject(boundary)) {

            removeChild(objects);

            objects.splice(i, 1);

            }

            catcher.x = mouseX;

            checkStageBorder();

            }

}

This topic has been closed for replies.
Correct answer kglad

test if the objects exist before trying to use them in your hittest.  for example you can use the following to check for all your hittests except the boundary.  how you should handle the boundary hit depends on whether more than one catcher.mouth_mc an exist at any one time:

public function moveObjects(e:Event) {

for (var i: int = objects.length -1; i >= 0; i--) {

objects.y += speed;

addChild(objects);

for(var j:int=2;j<=5;j++){

if(catcher["mouth_mc_"+j]){

if (objects.hitTestObject(catcher["mouth_mc_"+j])) {

      if (objects.typestr == "solid") {

      score += 5;

      soundfx_1.play();

      catcher["mouth_mc_"+j].gotoAndPlay(9);

      }

     else

     {

     score -= 5;

     soundfx_2.play();

     catcher["mouth_mc_"+j].gotoAndPlay(2);

     }

     if (score < 0) score = 0;

    ScoreDisplay.text = String(score);

    removeChild(objects);

    objects.splice(i, 1);

    }

}

2 replies

Participant
January 2, 2013

Whenever you splice an object in a for loop, I recommend using 'continue;' after it. Change the lines after

if( score < 0 )

to:

ScoreDisplay.text = String(score);

removeChild(objects);

objects.splice(i, 1);

continue;

Look for anywhere else you use 'objects.splice(i,1);' and type 'continue;' after it. I think the reason you are getting a null error is because the code is trying to check for an element in the array that isn't there anymore because you have removed it from the array. Also, I'd recommend typing 'objects = null' before removing it from the array to make sure you don't cause a leak in memory

Inspiring
January 2, 2013

The error still appears even though I have placed the continue function.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 1, 2013

test if the objects exist before trying to use them in your hittest.  for example you can use the following to check for all your hittests except the boundary.  how you should handle the boundary hit depends on whether more than one catcher.mouth_mc an exist at any one time:

public function moveObjects(e:Event) {

for (var i: int = objects.length -1; i >= 0; i--) {

objects.y += speed;

addChild(objects);

for(var j:int=2;j<=5;j++){

if(catcher["mouth_mc_"+j]){

if (objects.hitTestObject(catcher["mouth_mc_"+j])) {

      if (objects.typestr == "solid") {

      score += 5;

      soundfx_1.play();

      catcher["mouth_mc_"+j].gotoAndPlay(9);

      }

     else

     {

     score -= 5;

     soundfx_2.play();

     catcher["mouth_mc_"+j].gotoAndPlay(2);

     }

     if (score < 0) score = 0;

    ScoreDisplay.text = String(score);

    removeChild(objects);

    objects.splice(i, 1);

    }

}

Inspiring
January 1, 2013

I tried the code you gave and the objects exist and they only catches the solid objects. Can you please give me an idea about the correct way of hitTesting each mouth_mc movieclips? Thank you in advance.

kglad
Community Expert
Community Expert
January 1, 2013

what do you mean by, "they only catches the solid object"?