Search code examples
actionscript-3flashflash-cs3

TypeError: Error #2007


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()

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))