动态放置的MovieClip实例上的hitTestObject

时间:2010-09-11 20:07:51

标签: flash dynamic instance movieclip hittest

尝试在动态放置在舞台上的实例上执行hitTestObject。

收到错误:

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 个答案:

答案 0 :(得分:1)

就像我之前的told一样,你又犯了同样的错误,请改用这个代码:

 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");
            }
    }