Archive for August, 2008
Why some AS3 swfs work stand alone but fail to load into other swfs
Wednesday, August 13th, 2008 | flex | 12 Comments
Most developers who create standalone swfs have some standard instantiation code in their class constructor, something like this:
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
This works fine when the swf is embedded on a page directly as the “stage” property will be available instantaneously.
When this swf is loaded into another swf however, say like this:
var loader:Loader = new Loader();
addChild(loader);
loader.load(new URLRequest(”http://widget.meebo.com/mcr.swf?id=RQYRkTseLc”));
It generates the infamous and ambiguous: TypeError: Error #1009: Cannot access a property or method of a null object reference.
I’m pretty sure this is because when a swf is loaded into another swf, it’s class constructor is called to create the object before it is even added to it’s parent (the Loader object).
Thus the stage property is undefined, which throws the error, which kills the call stack including what ever else was in the constructor after the stage reference.
I’d kind of consider this a bug in the flash architecture.
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();
}
handy way of removing event listeners via arguments.callee
Saturday, August 9th, 2008 | actionscript | No Comments
Theo’s entry reminded me how useful this can be especially when using an anonymous function as a one time callback. Try it out:
stage.addEventListener(MouseEvent.MOUSE_DOWN, function(event:Event):void { event.currentTarget.removeEventListener(event.type, arguments.callee); trace(’ran once’); });
FYI you should always use event.currentTarget and not event.target when removing a mouse event listener because event.currentTarget will always reference the object that the listener was explicitly attached to. event.target can easily reference a different object like a child displayObject inside the displayObject you were listening for a bubbling event on.
Fix for when Flex and your debug flash player won’t connect for a debug session
Wednesday, August 6th, 2008 | Uncategorized | 3 Comments
Luckily this guy wasted hours finding a fix to this strange issue so I didn’t have to. Right click on the swf and click on “debugger” in the flash context menu (if the “Where is the debugger or host application running?” pop up isn’t already open). Select the “Other Machine” radio box button and type in “127.0.0.1″. Voila. Why does this work when “localhost” was already selected by default? No idea but i’ll be getting something done tonight. Thank you guy and your blog.
If this doesn’t solve your issue and your using Firefox 3 check out this Jira.