Error1010: A term is undefined and has no properties
this error has been driving me crazy! debug won't tell me term i'm having trouble with. below code, class defines movement , behavior of these items i'm creating on stage. i'm getting error twice, once avoidme function , other checkcollision function. avoidme makes these items move away movieclip user controls on stage, , checkcollision function checks if movieclip hits of these items , removes them off stage. me!
package { import flash.display.movieclip; import flash.events.event; import flash.geom.rectangle; public class itembehavior extends movieclip { var arandomnumber:number = randomnumber(1,3); //rectangle boundaries var xlocation:number = 0; var ylocation:number = 0; var widthofrect:number = 450; var heightofrect:number = 450; var rectboundary:rectangle = new rectangle(xlocation,ylocation,widthofrect,heightofrect); var itemspeed:number = 5; var mindistancebetweenitems:number = 20; public function itembehavior():void { //have each item have random position on stage this.x = math.round(math.random() * 500) + 20; this.y = math.round(math.random() * 500) + 20; //have each item have different size this.scalex = scalex * arandomnumber; this.scaley = scaley * arandomnumber; //add listener move items away cat addeventlistener(event.enter_frame,avoidcat); //add listener remove items stage when hit cat addeventlistener(event.enter_frame,checkcollision); } function randomnumber(min:number, max:number):number { return math.floor(math.random() * (1 + max - min) + min); } function avoidcat(event:event):void { // calculate distance between cat , items var distancex:number = movieclip(parent).cat_mc.x - this.x; var distancey:number = movieclip(parent).cat_mc.y - this.y; var distancexy:number = math.sqrt(distancex*distancex+distancey*distancey); // check if distance small enough if (distancexy <= mindistancebetweenitems) { // calculate direction between cat , items var currdirection:number = math.atan2(distancey,distancex); // move items opposite direction this.x -= itemspeed * math.cos(currdirection); this.y -= itemspeed * math.sin(currdirection); } //restrict items in rectangle boundaries if (this.x < xlocation) { this.x = widthofrect; } if (this.x > widthofrect) { this.x = xlocation; } if (this.y < ylocation) { this.y = heightofrect; } if (this.y > heightofrect) { this.y = ylocation; } } //function check collision , remove item function checkcollision(event:event):void { if (movieclip(parent).cat_mc.hittestobject(this)) { parent.removechild(this); removeeventlistener(event.enter_frame,checkcollision); } } } }
click file>publish settings>tick "permit debugging". retest.
the problematic line number be in error message.
p.s. may need use event.added_to_stage event before accessing code references parent. ie, before adding enterframe listeners.
More discussions in ActionScript 3
adobe
Comments
Post a Comment