Sunday, December 16, 2007

preloader absorbing application FlashVars

This blog has moved.


As has happened frequently, after completing a Flash application, I find that the size of the application demands a preloader swf to load the application swf (complete with progress bar). the problem i have found with this preloader swf is that it now absorbs any FlashVars that the application swf was expecting to be passed to it from the html.

how to get the preloader to pass the
FlashVars to the application? well, the preloader could extract the FlashVars and explicitly send them to the application in a function call once the application is loaded. but this would mean that the application would work differently than it would have prior to the loader. it could also mean that the preloader may have to be modified for each project, whereas a generic preloader would be much simpler.

here is a solution that means the application works exactly the same, and the preloader can remain generic - within the preloader extract all the
FlashVars, and pass them along to the application as URLVariables in the URLRequest.data property.

var ldr:Loader = new Loader();
var url:String = "application.swf";
var urlReq:URLRequest = new URLRequest(url);
urlReq.data=getObjectURLVariables(this.loaderInfo.parameters)
ldr.load(urlReq);
this.addChild(ldr);


public function getObjectURLVariables(whichObject:Object):URLVariables {
var variables:URLVariables=new URLVariables();
for (var i in whichObject) {
variables[i]=whichObject[i];
}
return(variables);
}

You could quite easily set this up as a custom Loader class that automatically passes the FlashVars along to the loaded application.

9 comments:

Unknown said...

I've not been able to get this to work.

Within the child SWF how do you reference the urlVariables.data?

I can't find the property that this would reference.

Craig Grummitt said...

The child swf would reference the flashVars in exactly the same way that the parent swf accesses the flashVars - through the loaderInfo.parameters property.

Tom said...

There's a typo...line 4 should read:

urlReq.data=getObjectURLVariables(this.loaderInfo.parameters)
ldr.load(urlReq);

This snippet is great...I often have to make the preloader afterwards! Thank you.

Craig Grummitt said...

Thanks Tom, I´ve fixed the typo, glad the snippet was helpful.

Unknown said...

So would my Main Document class remain unchanged? At the moment I've got

var paramObj:Object = this.loaderInfo.parameters;

In my Main file, and I've pasted your code into my preloader.

What else needs to change?

Thanks!

Joe

Craig Grummitt said...

Nothing, should work exactly as described in the post... why, are you having problems with it?

shub said...

great dude ... i wasted a whole day for that .. thanks fr ur post ..
keep posting :)

Neron said...

Hi, great post. I have a question for you :) .
Do you know if there is any trick to set the parameters when using loadBytes.
Already checked that there is a solution using LoaderContext.parameters but only works with Flash >= 10.2.

beawolf said...

Works like a charm really. I didn't change any single character in my main swf to use this.

Thanks a lot...:)