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.

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.

iOS development with Swift - book: https://manning.com/books/ios-development-with-swift video course: https://www.manning.com/livevideo/ios-development-with-swift-lv

Tagged with: , ,
Posted in Flash
9 comments on “preloader absorbing application FlashVars
  1. hobbbz says:

    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. 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 says:

    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. Thanks Tom, I´ve fixed the typo, glad the snippet was helpful.

  5. Joe says:

    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. Nothing, should work exactly as described in the post… why, are you having problems with it?

  7. shub says:

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

  8. Neron says:

    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 says:

    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 to hobbbz Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: