actionscript 3 - AS3 - How do you Ignore a class (Error #1009)? -
i have created class movieclip creating coins player (char) pick up. class suppose add score , coinscollected variables, , remove 1 coin stage. when change frame using gotoandstop(#);
, console spams
errortype: error #1009: cannot access property or method of null object reference."
coin class:
public class coin extends movieclip{ var char:movieclip; var maintimeline = movieclip(root); public function coin() { // constructor code this.addeventlistener(event.enter_frame,update); } function update(event:event):void{ if(maintimeline.currentframe!=5){ char=movieclip(root).char; if(this.hittestobject(char)){ this.removeeventlistener(event.enter_frame,update); parent.removechild(this); maintimeline.score++; maintimeline.coinscollected++; } } } }
root isn't populated until display object has been added display list. need listen event before setting variable.
var char:movieclip; var maintimeline; //do not initialize here, root null @ point public function coin() { // constructor code //root still null here too, see if it's populated yet if(root){ init(); //root populated, skip initialization }else{ this.addeventlistener(event.added_to_stage,addedtostage); //root isn't populated yet, listen added stage , initialize } } private function addedtostage(e:event = null):void { this.removeeventlistener(event.added_to_stage,addedtostage); init(); } private function init():void { maintimeline = movieclip(root) this.addeventlistener(event.enter_frame,update); }
Comments
Post a Comment