Craig Grummitt – Before I forget

Discoveries in Swift and iOS development.

preloader absorbing application FlashVars

·

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.

[sourcecode language=”actionscript3″]
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);
}
[/sourcecode]

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


Comments

  1. hobbbz Avatar

    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.

  2. Craig Grummitt Avatar

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

  3. Tom Avatar

    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.

  4. Craig Grummitt Avatar

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

  5. Joe Avatar

    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

  6. Craig Grummitt Avatar

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

  7. shub Avatar
    shub

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

  8. Neron Avatar
    Neron

    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.

  9. beawolf Avatar

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

    Thanks a lot…:)

Leave a Reply

Your email address will not be published. Required fields are marked *