Why some AS3 swfs work stand alone but fail to load into other swfs
Wednesday, August 13th, 2008 | flex | 3 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’); });
Fix for when Flex and your debug flash player won’t connect for a debug session
Wednesday, August 6th, 2008 | Uncategorized | 1 Comment
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.
How to launch Flex debug session via keyboard shortcut on mac (part deux)
Wednesday, July 30th, 2008 | Uncategorized | 1 Comment
So continuing on this post I took a gander at Flex > Preferences > Keys to get my command-f11 key working… with success! Under the list of keyboard shortcuts about halfway down the “>Category<” column is Run/Debug. The command “Run Last Launched” is mapped to Command-F11 as it should be but under the “When” column it is set to “In Windows”. Not sure what all “In Windows” entails but I edited this shortcut adding a new “When” to this shortcut found under the drop down list called “Editing Flex Source” and bam, functional command-f11. Sure beats the kung-fu move of a keyboard shortcut mentioned in my last entry.
How to launch Flex debug session via keyboard shortcut on mac
Tuesday, July 29th, 2008 | Uncategorized | 6 Comments
 
OMG. For over 2 years I’ve launched Flex by mousing over to the little bug icon and clicking. I’ve done it so often its become second nature. All because I took one look at this cryptic glyph sequence, tried to guess..tecute it, then gave up to pretend this shortcut didn’t exist.
After a brief stint back in the Flash IDE–instantly returnign to hitting command-enter to debug, I came back to Flex with the determination to slay this gnarly shortcut sequence once and for all. And here’s the secret:
That up arrow icon is shift. Thats an easy one. The bizzare serif-X-with-one-leg-missing glyph represents alt/option. Yeah I don’t understand that either. Then there’s the letter “D” and “F”. Naturally, hitting all 4 together does nothing. But wait, see the slight excess kerning between those 2 letters? Thats a space. It means let go of the current key combination before hitting the next letter. And there it goes.
Why 4 keys in 2 combinations just to debug your project? Seams a little absurd for one of the most important and common shortcuts. Is every other normal key combination really taken? Really? I’m using the Eclipse plugin version of Flex btw, but I assume its the same with the stand alone.
Did anyone else have trouble with this? Don’t make me feel stupid and annoyed all by myself :P
UPDATE: Figured out how to get the more straight forward command-f11 shortcut working here
Serious (and seriously obscure) bug with flash embed code redirects and variables in IE
Thursday, July 24th, 2008 | Uncategorized | 4 Comments
Haven’t been posting too frequently, been pretty busy at KickApps working on the Widget Studio. I’d like to write about it in another entry at another time but for now I’d like to help expose a pretty vicious bug we’ve encountered during the course of developing this app.
The Widget Studio is a Flex based WYSIWYG widget generator who’s end product is embed code for widgets. The embed src points to a server side redirect that returns our main SWF file with some variables appended to it. These variables have to be dynamic to pass things like the build version number (cache control) and refferal url to the swf. After much debugging we realized if querystring variables are appended to the redirect in <embed src=”"/> then variables attached to the swf from inside the redirect do not get passed to the SWF in IE. After much googling, these are the only 2 entries we could find on the matter:
MSDN forum post
JW Player (?) forum post
If both appended-to-redirect and inside-redirect variables are neccessary, the only viable answer is a rest style architecture to pass variables to your SWF (assuming, like in our case, the appended-to-redirect variables can’t be moved to flashVars as they are used by the redirect script itself)
Event.INIT and Event.COMPLETE don’t fire when loading an unpacked swc
Thursday, July 24th, 2008 | Uncategorized | No Comments
Cleaning out the wordpress there were a few posts I hadn’t finished. This one is mainly a for-future-reference for myself.
I’ve been doing some run-time class loading involving loading SWF files into the same ApplicationDomain as the container via Loader then using getDefinitionByName to access new instances of the loaded classes. I was perplexed that the INIT and COMPLETE events weren’t being dispatched and found some info here.
SecurityError: Error #2000: No active security context.
Thursday, July 24th, 2008 | Uncategorized | No Comments
Always interesting to throw an error that has only 4 obscure results in google.
Using setTimeout with navigateToURL:
var urlRequest:URLRequest = new URLRequest(”http://google.com”);
setTimeout(navigateToURL, 1000, urlRequest, “_blank”);
Generates:
SecurityError: Error #2000: No active security context.
at global/flash.net::navigateToURL()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at <anonymous>()
at SetIntervalTimer/onTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
A work around is to wrap the call in an anonymous function:
setTimeout(function():void { navigateToURL(urlRequest, “_blank”); }, 1000);
Fix for Eclipse tab reordering “feature” (MRU tab positioning policy)
Tuesday, March 18th, 2008 | Uncategorized | No Comments
I love using Eclipse for my day to day but I hate how it automatically re-orders my tabs. It looks like other people view the Eclipse “MRU” tab positioning policy as a burden as well.
If you know what I’m talking about and want your tabs to remain where you put them, try installing this Eclipse plugin made to do exactly that: Extended VS Presentation plugin
Its got some other really nice features but check it out and see for yourself.
How to make an efficient image proxy
Thursday, February 21st, 2008 | actionscript, bugs, help | 4 Comments
So the work I’m currently doing is centered around loading data from different domains. I’ve got a proxy setup to get around pesky crossdomain issues but I’ve been trying to make it a bit dynamic for efficiency’s sake. I only want to proxy if I know for sure that the data isn’t coming from our servers or a server with a crossdomain file in place.
Creating a “smart” proxy with the URLLoader for text content is a piece of cake. Simply listen for the SecurityErrorEvent then run the url through the proxy. Beautifully simple.
For the Loader class its a different story. The Loader class’s contentLoaderInfo doesn’t have a security error event, just an ioError event. Your supposed to use the childAllowsParent property of the contentLoaderInfo to figure out whether you have bitmapData level access. The only problem is you can’t access that property until the Event.COMPLETE event is dispatched despite the livedocs LoaderContext page stating this property is available on ProgressEvent.PROGRESS which will throw a Error #2099: The loading object is not sufficiently loaded to provide this information. at flash.display::LoaderInfo/get childAllowsParent()
I can finagle with the LoaderContext and try/catches all I want but there doesn’t seam to be a creative solution to this problem. The result: I have to load every image that doesn’t have a policy file TWICE. Once to check childAllowsParent, the next to proxy the image. This doesn’t make any sense considering on the same livedocs LoaderContext page it states:
When you call the Loader.load() method with LoaderContext.checkPolicyFile set to true, Flash Player does not begin downloading the specified object in URLRequest.url until it has either successfully downloaded a relevant cross-domain policy file or discovered that no such policy file exists.
I can see the “Error: Request for resource at http://… by requestor from http://… is denied due to lack of policy file permissions.” errors being outputted to the trace window but I don’t seam to have a way to catch it in code. Is something as significant as this, really a bug? Can anyone give me a hand?