I've read the previous posts of SO and it seems that some people's had the same issue as me - but none came up with a solution that helped my problem :)
I've designed 3 games that are their own SWF file, and I'm trying to load them upon a keypress in a container-program, sort of :).
I'm getting the TypeError 1009 null reference on all my SWF files though :( and they run perfectly when I load them from Windows. I've tried loading a SWF made by someone else, and that did work. So maybe my way of designing flash games is a bit off? Everything takes place in frame 1 - that's maybe something that's off.
My loader code is:
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeysDown);
function loaderFunction(swfFile:String):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(swfFile));
}
function onComplete(e:Event):void {
var movie:* = LoaderInfo(e.currentTarget).content;
//Adding content to the stage
stage.addChild(movie);
}
function reportKeysDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.NUMBER_1) {
stage.removeChildren(1);
loaderFunction("Card.swf");
}
if(event.keyCode == Keyboard.NUMBER_2){
stage.removeChildren(1);
loaderFunction("Puzzle.swf");
}
if(event.keyCode == Keyboard.NUMBER_3){
stage.removeChildren(1);
loaderFunction("BallGame.swf");
}
}
And the error is
TypeError: Error #1009: Cannot access a property or method of a null object reference. at BallGame_fla::MainTimeline/frame1()
(I get these for everyone of the SWF's)
I'm not sure how to debug those games I'm loading since there's nothing wrong when I play separately. Any ideas :)? Thanks!
Edit: It started telling me which lines were acting up, and it seems to be the keyboard-listener, at least for one of the games. I'm reading keyboard into much like I am with the loader swf.
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
Solution-Edit: The problem is that the loaded SWFs are using the "stage" property, and when they're loaded into another SWF the stage isn't theirs to own. That's what's causing the errors :) @BotMaster
Solution-Solution Edit: You can still look for keyboard inputs (like I was doing) in a nested SWF, if you're using the
Event.ADDED_TO_STAGE
Like this, for instance :)
this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
function onAdded(e:Event):void{
trace("added");
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
}
@null
stage
only becomes available (it's not null
) when the DisplayObject
is added to the display list.
The only exception to that rule is the main file or the first file that is opened in the flash player. That's why each of your games plays fine individually.
You should only use stage
property if it is available. To do this, listen for the Event.ADDED_TO_STAGE
event. This will be dispatched when something is added to the stage. In the handler function, you can safely use stage
.
You still should add the listener to the stage
but only if it is available to you.
Please read the documentation about addChild(). You should basically never add anything to stage
. In general, take a look at the basics of the display list and how it works.