flash - Actionscript 3 returns Error #1009 -
i creating shooting game. have encountered problem code when decided create method remove missile after reaching top of stage. can run program without issues have realize missile not remove away stage, if hold shooting button. however, if tap shooting button, missile removed away error #1009 printing out of output.
is there solution fix problem?
here's error after missile flew top of stage debugging enabled:
typeerror: error #1009: cannot access property or method of null object reference. @ missle/destroymissle()[e:\experiment\experimentproject\missle.as:39] @ main/checkmissleoffscreen()[e:\experiment\experimentproject\main.as:63] @ main/eventupdated()[e:\experiment\experimentproject\main.as:51]
here's main's class:
package { import flash.display.movieclip; import flash.events.event; import flash.events.keyboardevent; /** * ... * @author test */ public class main extends movieclip { //objects public var rect:movieclip; public var missle:missle; //array private var misslearray:array; //keyboard section var leftkeyisdown:boolean; var rightkeyisdown:boolean; var upkeyisdown:boolean; var downkeyisdown:boolean; var spacekeyisdown:boolean; //speed var characterspeed:number = 15; //main constructor public function main() { //array initializer misslearray = new array(); missle = new missle(); //update events listeners. stage.addeventlistener(event.enter_frame, eventupdated); //update keyboard events listeners stage.addeventlistener(keyboardevent.key_down, keypressed); stage.addeventlistener(keyboardevent.key_up, keyunpressed); } //events functions //this functions updated everytime object move private function eventupdated(e:event):void { playermoving(); playerclampmoving(); checkmissleoffscreen(); } private function checkmissleoffscreen():void { (var = 0; < misslearray.length; i++ ) { var currentmissle:missle = misslearray[i]; if (currentmissle.y < 0) { misslearray.splice(i, 1); missle.destroymissle(); } } } private function playerclampmoving():void { if (rect.x < 0) { rect.x = 0; } if (rect.x > stage.stagewidth - rect.width) { rect.x = stage.stagewidth - rect.width; } if (rect.y < 0) { rect.y = 0; } if (rect.y > stage.stageheight - rect.height) { rect.y = stage.stageheight - rect.height; } } private function playermoving():void { if (leftkeyisdown == true) { rect.x -= characterspeed; } if (rightkeyisdown == true) { rect.x += characterspeed; } if (upkeyisdown == true) { rect.y -= characterspeed; } if (downkeyisdown == true) { rect.y += characterspeed; } if (spacekeyisdown == true) { shootingmissle(); } } private function shootingmissle():void { missle = new missle(); missle.x = rect.x + (rect.width / 2); missle.y = rect.y; misslearray.push(missle); trace(misslearray.length); stage.addchild(missle); } //keyboard functions //check see whether user releases keyboard private function keyunpressed(e:keyboardevent):void { if (e.keycode == 37) { leftkeyisdown = false; } if (e.keycode == 39) { rightkeyisdown = false; } if (e.keycode == 40) { downkeyisdown = false; } if (e.keycode == 38) { upkeyisdown = false; } if (e.keycode == 32) { spacekeyisdown = false; } } //check see whether user presses keyboard private function keypressed(e:keyboardevent):void { if (e.keycode == 37) { leftkeyisdown = true; } if (e.keycode == 39) { rightkeyisdown = true; } if (e.keycode == 40) { downkeyisdown = true; } if (e.keycode == 38) { upkeyisdown = true; } if (e.keycode == 32) { spacekeyisdown = true; } } } }
here's missle's class:
package { import flash.display.sprite; import flash.events.event; /** * ... * @author test */ public class missle extends sprite { public function missle() { addeventlistener(event.added_to_stage, onadd); } private function onadd(e:event):void { removeeventlistener(event.added_to_stage, onadd); //objects on stage init(); } private function init():void { addeventlistener(event.enter_frame, misslelaunch); } private function misslelaunch(e:event):void { this.y -= 15; } public function destroymissle():void { parent.removechild(this); removeeventlistener(event.enter_frame, misslelaunch); } } }
try this:
public function destroymissle():void { if(parent !== null) parent.removechild(this); removeeventlistener(event.enter_frame, misslelaunch); }
it possibility calling .destroymissile()
more once meaning parent
null because you've removed stage , has no parent.
Comments
Post a Comment