Question

Sept 20,2012, 7:27pm GMT+8.. still having the same error even with the code "continue;"... any other suggestion? :(

HELP the previous answers still isnt working.. :(

praticing tutorial. im getting a error on this part of my code when the object git the catcher..

function moveObject(e:Event):void {
    // cycle thru objects using a for loop
    for (var i:int=objects.length-1; i>=0; i--) {
        //move objects down based on speed
        objects[i].y += speed;
        objects[i].rotation += speed;
        // if objects leaves the stage
        if (objects[i].y > 400) {
            // remove it from display list
            removeChild(objects[i]);
            // remove it from the array backwards
            // remove it backwards removes all problems when looping through.
            // forward would cause confusion if you removed 5 before 4. 
            objects.splice(i, 1);

        }
        if (objects[i].hitTestObject(player.arrowS.sackC)) {
            if (objects[i].typestr == "good") {
                score += 3;
            } else {
                score -= 5;
                if (score <= 0) {
                    score = 0;
                }
            }
            trace(score);
            updateScore(score);
            removeChild(objects[i]);
            objects.splice(i, 1);
        }
    }
  }

although the game is still working its irritating to see this. this is the error

TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display::DisplayObject/hitTestObject()
at adventure_fla::MainTimeline/moveObject()
Was it helpful?

Solution

You're looping through an array, and if if (objects[i].y > 400) { is true, then you splice that element out of your array, so objects[i] is now null.

Then you go and do if (objects[i].hitTestObject(player.arrowS.sackC)) { but if the first coniditon was true, objects[i] no longer has a value so you get the posted error.

What you want to do, is not break out of the loop, but rather use the continue keyword so the loop moves on to the next item.

if (objects[i].y > 400) {
        // remove it from display list
        removeChild(objects[i]);
        // remove it from the array backwards
        // remove it backwards removes all problems when looping through.
        // forward would cause confusion if you removed 5 before 4. 
        objects.splice(i, 1);
        continue; //abandon the rest of this loop cycle (since objects[i] is now null), and move on to the next loop cycle, so the below code doesn't execute for the current i
}

In addition to the above, you should also check that your player.arrowS.sackC isn't null:

if(player && player.arrowS && player.arrowS.sackC && objects[i].hitTestObject(player.arrowS.sackC))

OTHER TIPS

This error occurs when the value of a particular variable is null at runtime.Check hitTestObject value at different locations if it becomes null.

function moveObject(e:Event):void {
    // cycle thru objects using a for loop
    for (var i:int=objects.length-1; i>=0; i--) {
        //move objects down based on speed
        objects[i].y += speed;
        objects[i].rotation += speed;
        // if objects leaves the stage
        if (objects[i].y > 400) {
            // remove it from display list
            removeChild(objects[i]);
            // remove it from the array backwards
            // remove it backwards removes all problems when looping through.
            // forward would cause confusion if you removed 5 before 4. 
            objects.splice(i, 1);
            //once an element is spliced from the array the array is getting refreshed
            //so to avoid accessing empty array(ie, after the last element is removed) in the next if condition, the loop has breaked here
            //Don't worry about return statement as the Enter frame listener function calls till the listener has been removed
            return;
        }
        if (objects[i].hitTestObject(player.arrowS.sackC)) {
            if (objects[i].typestr == "good") {
                score += 3;
            } else {
                score -= 5;
                if (score <= 0) {
                    score = 0;
                }
            }
            trace(score);
            updateScore(score);
            removeChild(objects[i]);
            objects.splice(i, 1);
            return;
        }
    }
}

You need to store a reference to the object and work from that reference

 function moveObject(e:Event):void {
    // cycle thru objects using a for loop

    if(player.arrowS.sackC){
      var currentObject:MovieClip;
      for (var i:int=objects.length-1; i>=0; i--) {
          //move objects down based on speed
          currentObject = objects[i] as MovieClip;
          currentObject.y += speed;
          currentObject.rotation += speed;
          // if objects leaves the stage
          if (currentObject.y > 400) {
              // remove it from display list
              removeChild(currentObject);
              // remove it from the array backwards
              // remove it backwards removes all problems when looping through.
              // forward would cause confusion if you removed 5 before 4. 
              objects.splice(i, 1);
              continue;// go to next iteration of loop
          }
          if (currentObject.hitTestObject(player.arrowS.sackC)) {
              if (currentObject.typestr == "good") {
                  score += 3;
              } else {
                  score -= 5;
                  if (score <= 0) {
                      score = 0;
                  }
              }
              trace(score);
              updateScore(score);
              removeChild(currentObject);
              objects.splice(i, 1);
              continue;// go to next iteration of loop
          }
      }
    }
  }

sir i got the answer to solve my problem it isnt the code but in the animation that i made for the character, because it contains keyframe that both have the instance name sackC :D

i know it is not the professional way o do the coding but what can i say.. im just a beginner..

though thank you all for your assistance ..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top