2

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.

coner
  • 270
  • 7
  • 21

4 Answers4

4

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.

akmozo
  • 9,829
  • 3
  • 28
  • 44
1

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

Daniil Subbotin
  • 6,138
  • 5
  • 20
  • 24
  • Little confusion here. -Does this mean that method also works child to parent? -or is using 'loaderInfo.loader.loaderInfo.content' better? – coner Mar 18 '15 at 13:11
  • 1
    Use `loaderInfo.loader.loaderInfo.content` to pass variable from child to parent. Use `loader.content['level'];` to pass variable from parent to child. You can't use `loader.content['level'];` to pass variable from child to parent because you don't have `loader` in the child. – Daniil Subbotin Mar 18 '15 at 13:25
  • Oh, gotcha. It is great to hear the basic logic works both ways. Thanks for the answer. – coner Mar 18 '15 at 13:28
1

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;
BotMaster
  • 2,233
  • 1
  • 13
  • 16
0

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.

blue112
  • 52,634
  • 3
  • 45
  • 54
  • It throws unhandled IOError (#2044) and URL not found (#2035). The path is correct but... – coner Mar 18 '15 at 12:50