views:

29

answers:

1

Trying to do hitTestObject on instances that are dynamically placed on the stage.

Receiving an error:

TypeError: Error #2007: Parameter hitTestObject must be non-null.
    at flash.display::DisplayObject/_hitTest()
    at flash.display::DisplayObject/hitTestObject()
    at eatCircle()

ActionScript:

    var circ; 
    var circle_ary:Array = new Array;   
    var circ_num:int;//number of circles on the stage

   // function with hitTestObject not working ---------------------------------
    function eatCircle(event:Event):void {
        if (player_ary[me].hitTestObject(circle_ary[circ])) { // does not work
                trace ("I ate the circle");
                removeChild(circle_ary[circ]); // does not work
            } else {
                trace ("I didn't eat the circle");
            }
    }        

// creates instances of the Circle class and are randomly placed on the stage
    var circ_num:int=5;//number of circles on the stage

                for(var i=0; i<circ_num; i++) {
                    circle_ary[i] = new Circle(); //linkage in the library
                    addChild(circle_ary[i]);

                    circle_ary[i].x = Math.random() * (stage.stageWidth -circle_ary[i].width);
                    circle_ary[i].y = Math.random() * (stage.stageHeight -circle_ary[i].height);
                }
+1  A: 

as I told you before, you are making the same mistake again, use this snippet instead:

 function eatCircle(event:Event):void {
        if((player_ary[me]!=null)&&(circle_ary[circ]!=null)&&(player_ary[me].hitTestObject(circle_ary[circ]))) {
                trace ("I ate the circle");
                removeChild(circle_ary[circ]);
            } else {
                trace ("I didn't eat the circle");
            }
    } 
Eugene
let us know if it works for you.
Eugene
Received a couple of errors: 1084: Syntax error: expecting identifier before notequals. if(circle_ary[circ])!=null)
jc70
1008: Attribute is invalid. if (player_ary[me].hitTestObject(circle_ary[circ])) {
jc70
1083: Syntax error: else is unexpected. } else {
jc70
code is updated. check again
Eugene