coner
coner

Reputation: 270

Passing values between SWFs

I can reach child SWF's var from parent SWF but I would like to pass parent var to child to inform and operate different things in child (depends on var, basic if condition). Is it possible to load SWF and pass a var or value with it?


private function callFile()
{
    loader = new Loader;
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
    loader.load(new URLRequest("main.swf"));
}

private function onLoaded(e:Event)
{
    level_no = loader.content['level'];
}

This is parent->child variable read. And I want it two-way transfer.

Upvotes: 2

Views: 470

Answers (4)

akmozo
akmozo

Reputation: 9839

To pass a parameter between a "parent" swf to its loaded "child" swf, you can use a LoaderContext object to do that like this :

loader.swf (parent) :

var context:LoaderContext = new LoaderContext();
    context.parameters = {'passed_param': 'value'};

var loader:Loader = new Loader();
    loader.load(new URLRequest('loaded.swf'), context);
    addChild(loader);

loaded.swf (child) :

trace(this.loaderInfo.parameters.passed_param); // gives : value

Hope that can help.

Upvotes: 4

BotMaster
BotMaster

Reputation: 2223

Any public defined method or variable in the swf can be called or accessed. If you define a method named doSomething in your swf then you can do:

MovieClip(loader.content).doSomething(myParentVariable);
//or
var level:int = MovieClip(loader.content).level;

Upvotes: 1

Daniil Subbotin
Daniil Subbotin

Reputation: 6708

In the child SWF instead of loader.content['level']; use loaderInfo.loader.loaderInfo.content['myVar'];. myVar is an variable defined in the parent SWF.

Upvotes: 1

blue112
blue112

Reputation: 56412

You can just use the query parameter to load your swf :

loader.load(new URLRequest("main.swf?var=17&var2=lol"));

Then, in your child swf, use :

stage.root.loaderInfo.parameters.var
stage.root.loaderInfo.parameters.var2

It will do the job.

Upvotes: 0

Related Questions