so you have 2 AS3 SWFs... lets say one is a website and another is a mini game. The game works fine on its own. The website SWF will load the game in. You test it, and you suddenly get this error: > TypeError: Error #1009: Cannot access a property or method of a null object reference.
Weird - cos the game worked on its own.... so you start tracing and debugging and realise that the code in the game's constructor is trying to use the stage (to position things in the middle or whatever) and for some reason stage == null, when previously it was dandy.
The work around to insure your swf is compatible with loading into other swfs: If the stage references are just the above, you can throw a try/catch around them as they would probably get set by the loader anyways:
try { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; } catch(er:Error) { };
or just ensure the property is available:
if (stage){ stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; }
or, if there is setup that absolutely requires the stage reference, put this in your constructor:
Constructor(){ if (stage){ onAddedToStage(); } else { addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } }
private function onAddedToStage(evt:Event=null):void { removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; initApp(); }
of course then you realise that using stage to layout out stuff in the game won't work anymore anyway, cos the stage in the website swf is bigger and that buggers up the alignment... but that's another story.
Hey DBM,
How did you handle the sizes in a nested sprite?
I was thinking of passing my nested sprite a max width and height at this event.
?
in the end I put a block in the background of the SWF that I loaded in and I used that block to work out the size of the movie - that way I could center stuff relative to the loaded SWF instead of relative to the stage which was much larger and not the same aspect ratio. I didn't use the ADDED_TO_STAGE event in the end, cos I'd got rid of the need to reference the stage.
ah, good idea.
Thanks!