Error #1009: Cannot access a property or method of a null object reference.
hello,
i have 1009 error can't seem find fix for! have movieclip of cat on stage, cat_mc, reset button, reset_btn. document class following , adds 4 different items randomly on stage:
package { import flash.display.*; import flash.utils.*; import flash.events.*; public class ghneimsex6 extends movieclip { public function ghneimsex6() { //make holder items var catitemsholder = new movieclip(); addchild(catitemsholder); //create items inside holder var catitems:array = ["fish","heart","medicine","yarn"]; makeitems(catitems); var classreference:class; function makeitems(catitems) { //iterate through each item in list (var prop in catitems) { //create random number of items each var itemsnum:number = 30 + math.round(math.random() * 5); //create items dynamically attaching them holder movie clip (var i=0; i<itemsnum; i++) { //this needed convert string class reference can //create objects library. classreference = getdefinitionbyname(catitems[prop]) class; var newcatitem = new classreference(); //increment depth next item catitemsholder.addchildat(newcatitem, 1); } } } //reset button reset items on stage reset_btn.addeventlistener(mouseevent.click,resetitems); function resetitems(myevent:mouseevent) { removechild(catitemsholder); catitemsholder = new movieclip(); addchild(catitemsholder); makeitems(catitems); } } } }
i getting error in custom class, of each of items adding on stage , following:
package { import flash.events.*; import flash.display.*; import flash.geom.rectangle; public class itembehavior extends movieclip { var arandomnumber:number = randomnumber(1,2); //rectangle boundaries var xlocation:number = 10; var ylocation:number = 10; var widthofrect:number = 450; var heightofrect:number = 450; var rectboundary:rectangle = new rectangle(xlocation,ylocation,widthofrect,heightofrect); var itemspeed:number = 7; var mindistancebetweenitems:number = 90; public function itembehavior():void { //have each item have random position on stage this.x = math.round(math.random() * 470); this.y = math.round(math.random() * 470); //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() * ((0.1 + max) - min) + min); } function avoidcat(event:event):void { // calculate distance between cat , items var distancex:number = this.x - movieclip(parent).cat_mc.x; var distancey:number = this.y - movieclip(parent).cat_mc.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 = xlocation; } if (this.x > widthofrect) { this.x = widthofrect; } if (this.y < ylocation) { this.y = ylocation; } if (this.y > heightofrect) { this.y = heightofrect; } } //function check collision , remove item function checkcollision(event:event):void { if (movieclip(parent).cat_mc.hittestobject(this)) { removeeventlistener(event.enter_frame,checkcollision); removeeventlistener(event.enter_frame,avoidcat); parent.removechild(this); } } } }
i receiving errors custom class, itembehavior @ lines 44, , 84, following respectively:
var distancex:number = this.x - movieclip(parent).cat_mc.x;
if (movieclip(parent).cat_mc.hittestobject(this))
i made sure go stage before cat_mc it's still giving me null object error.
i receiving error addchildat function says following:
rangeerror: error #2006: supplied index out of bounds.
at flash.display::displayobjectcontainer/addchildat()
at function/ghneimsex6/$construct/makeitems()
what complete error message(s) 1009 error(s). before testing go flash publish settings , select option permit debugging.
for 2006 error guess trying add index 1 of empty catitemsholder. have start @ 0.
catitemsholder.addchildat(newcatitem, 1);
More discussions in ActionScript 3
adobe
Comments
Post a Comment