Jump to content
Search Community

Passing parameters to subloading swf

GreenSock test
Moderator Tag

Recommended Posts

I got an e-mail asking how one could pass some custom parameters to a subloaded swf that normally gets those parameters via the stage.loaderInfo.parameters object (but in this case, they wanted to have the subloaded swf ignore the stage.loaderInfo.parameters and instead use some custom values that are defined in the SWFLoader). I figured I'd post a technique here in case it's helpful to anyone else.

 

Let's say you want to pass two parameters named "param1" and "param2" (use whatever names you want). In the parent swf, you could just put the parameters in the SWFLoader's "vars" object like this:

 

var loader:SWFLoader = new SWFLoader("child.swf", {param1:"test1", param2:"test2", container:this});
loader.load();

 

Then, in the child swf, you could have it look for the parameters in "this.loaderInfo.parameters" first (so that it runs fine in standalone mode) and then if the values aren't found there, you could recursively look up the parent chain and see if you find a ContentDisplay object which would be characterized by having "loader" and "rawContent" properties. Then look for the associated values in the "vars" object of the SWFLoader. You could also define some default values in case none are found:

 

var params:Object = {param1:"default1", param2:"default2"};

if (this.loaderInfo.parameters.hasOwnProperty("param1") && this.loaderInfo.parameters.hasOwnProperty("param2")) {
params = this.loaderInfo.parameters;
} else {
var curParent:DisplayObjectContainer = this.parent;
while (curParent) {
	if (curParent.hasOwnProperty("loader") && curParent.hasOwnProperty("rawContent")) {
		params = Object(curParent).loader.vars;
		break;
	}
	curParent = curParent.parent;
}
}

init(params.param1, params.param2);

function init(param1:String, param2:String):void {
trace("init( " + param1 + ", " + param2 + " )");
}

 

Of course it would technically be "cleaner" to check if (curParent is ContentDisplay) instead of if (curParent.hasOwnProperty("loader") && curParent.hasOwnProperty("rawContent")) but the former way would force ContentDisplay and some core LoaderMax classes to get compiled with the child swf (bloating the file size), so the latter technique keeps file size to a minimum. Use whichever method you prefer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...