I started working on a flash shoot em-up game tutorial and i finished it Asgamer Shoot em upp Game
Now i started creating a new .fla that is the Main Menu and i have a play button so when i press it it will load the .swf(Game swf) but when i press the button i get the following error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
-at com.senocular.utils::KeyObject/construct()
-at com.senocular.utils::KeyObject()
-at com.actionscript.Ergasia::Ship()
-at com.actionscript.Ergasia::Engine()
public function Engine() : void {
if(stage) {
initialize();
} else {
addEventListener(Event.ADDED_TO_STAGE,initialize);
}
}
private function initialize(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE,initialize);
// here goes the code that's currently in Engine constructor
}
EDIT: Thanks to Viper for solving this!
stagealready assigned, while in this content itsstageproperty is yet null, as it's loaded but not added anywhere. So, wherever you addressstagein your game SWF, make anEvent.ADDED_TO_STAGElistener, and put all code that accesses stage in there, also don't allow proceeding while stage is null. Also, it's not enough to initiate loading the SWF, you ave to wait until it'll complete loading. - VesperShipclass. Do revise your entire codebase for constructors that call forstage.somethinginside them, these will net you a 1009 error if you let them. Instead, slap aif (stage) init(null); else addEventListener(Event.ADDED_TO_STAGE,init);line at the end of each of these, and implement afunction init(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE,init); ...}that will process whatever your class needs from stage. - VesperEngineclass has a pre-placed instance on the stage at design time. Right, this is a common issue of addressing stage in constructors. Yes, you need to do exactly this, except for makingEngine()return void - it's still a constructor. So,public function Engine() {instead of: void. - Vesper