actionscript 3 - AS3 Error 1009 - Where am i going wrong? -
this error i'm getting:
typeerror: error #1009: cannot access property or method of null object reference. @ ica_v7_fla::maintimeline/addegg() @ function/http://adobe.com/as3/2006/builtin::apply() @ setintervaltimer/ontimer() @ flash.utils::timer/_timerdispatch() @ flash.utils::timer/tick()
cannot see going wrong - point out i'm trying refer null object reference please :)
the rest of reads:
import flash.events.event; import flash.utils.timer; import flash.events.timerevent; //set variable can changed @ later //date user level can change (if necessary). var eggmovespeed=6; var score=0; var scorevalue=10; var level=1; var nextlevel=100; var eggcount=0; var eggmax=15; btn.addeventlistener(mouseevent.click, scorer); function scorer(e:event):void{ //add score. score=score+10; //display score in text box. scoretext.text=score; } var eggadd = setinterval(addegg,2000); function addegg(){ //add egg count (to ensure maximum not exceeded). eggcount=eggcount+1; if (eggcount<=eggmax){ //create object of egg_mc egg class. var egg:egg_mc = new egg_mc(); //set max , min width positions. var maxwidth = 452; var minwidth = 98; //randomize position of egg on screen - http://www.kirupa.com/developer/actionscript/tricks/random.htm var positionx = math.floor(math.random()*(1+maxwidth-minwidth))+minwidth; //position egg on stage, , add screen egg.y=400; egg.x=positionx; //add egg stage. stage.addchild(egg); //add moving loop egg. egg.addeventlistener(event.enter_frame, loop); }else{ clearinterval(eggadd); } function loop(e:event):void{ //move egg screen. egg.y-=eggmovespeed; //check see if egg has got top of screen - if so, move object bottom. if (egg.y<-100){ egg.y=400; } } //add event listener egg, see if has been clicked. egg.addeventlistener(mouseevent.click, clickedegg); function clickedegg(e:event):void{ //http://asgamer.com/2009/flash-game-design-basics-adding-library-objects-to-stage-with-as3 //create object of brokenegg_mc broken egg class. var brokenegg:brokenegg_mc = new brokenegg_mc(); //position brokenegg image wherever egg image was. brokenegg.y=egg.y; brokenegg.x=egg.x; //add brokenegg stage, , remove egg image. stage.addchild(brokenegg); stage.removechild(egg); //add score. score=score+scorevalue; //display score in text box. scoretext.text=score; //set levelcheck variable 0 - recalculate correctly. var levelcheck = 0; //check level user on dividing score next level required score. levelcheck=score/nextlevel; //setup variable use actual level display. var actuallevel=0; //check see if levelcheck variable has come less 1 (i.e. 0.1 or score of 10). if (levelcheck < 1){ //if yes, set level 1, user still on level 1. actuallevel=0; } else { //if not, round down lowest level (1.9 still level 1!). actuallevel=math.floor(levelcheck); } //display lowest level user. leveltext.text=actuallevel; }}
obviously, timer event loops until there 15 eggs on stage, interval should stop running - produces error.
your egg
variable function-wide, namely it's defined in addegg()
function, refer egg
inside clickedegg
, loop
, don't have actual access egg
's value. also, if there more single egg, how plan on tracking egg clicked, etc? @ least, use e.target
in clickedegg
function find out egg clicked, , don't forget clearing event listener.
function addegg():void { if (eggcounter>=eggmax) return; // why adding eggs if there many? var egg:egg_mc=new egg_mc(); // rest of code intact } function loop(e:event):void { var egg:egg_mc=(e.target egg_mc); if (!egg) return; // loop eggs // rest of function intact } function clickedegg(e:mouseevent):void { var egg:egg_mc=(e.target egg_mc); if (!egg) return; // rest of function intact, have valid egg link egg.removeeventlistener(event.enter_frame,loop); egg.removeeventlistener(mouseevent.click,clickedegg); // clear listeners. these persistent until removed manually! }
Comments
Post a Comment