开发者

hitTestObject on dynamically placed MovieClip instances

开发者 https://www.devze.com 2023-01-16 00:03 出处:网络
Trying to do hitTestObject on instances that are dynamically placed on the stage. Receiving an error:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消