Skip navigation
Currently Being Moderated

Parameter hitTestObject must be non-null.

Jan 1, 2013 8:17 AM

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[i].y += speed;

addChild(objects[i]);

 

if (objects[i].hitTestObject(catcher.mouth_mc_2)) {

      if (objects[i].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[i]);

    objects.splice(i, 1);

    }

 

   else if (objects[i].hitTestObject(catcher.mouth_mc_3)) {

         if (objects[i].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[i]);

      objects.splice(i, 1);

      }

 

     else if (objects[i].hitTestObject(catcher.mouth_mc_4)) {

            if (objects[i].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[i]);

           objects.splice(i, 1);

           }

 

           else if (objects[i].hitTestObject(catcher.mouth_mc_5)) {

            if (objects[i].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[i]);

            objects.splice(i, 1);

            }

 

    else if (objects[i].hitTestObject(boundary)) {

            removeChild(objects[i]);

            objects.splice(i, 1);

            }

 

            catcher.x = mouseX;

            checkStageBorder();

            }

}

 
Replies
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 1, 2013 8:34 AM   in reply to cams_zalzos

    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[i].y += speed;

    addChild(objects[i]);

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

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

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

          if (objects[i].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[i]);

        objects.splice(i, 1);

        }

    }

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 1, 2013 9:04 AM   in reply to cams_zalzos

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

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 1, 2013 9:33 AM   in reply to cams_zalzos

    well, why do you have that code in place if you don't want it?  what is the typestr property?  why don't you remove that code?

     

    and what objects are not "caught"?

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 1, 2013 5:50 PM   in reply to cams_zalzos

    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[i]);

    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[i] = null' before removing it from the array to make sure you don't cause a leak in memory

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points