AS3 Pass FlashVars to loaded swf
Asked Answered
H

4

7

I have a A.swf which loads B.swf onto a movieclip and needs to pass it some FlashVars. When loading B.swf with html, I can pass FlashVars fine. When passing from A.swf, it gets a

Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: file:

The code in A.swf is

var request:URLRequest = new URLRequest ("B.swf");

var variables : URLVariables = new URLVariables();
variables.xml = "test.xml";

// This line causes the error 2044, else B.swf loads fine with FlashVars  
request.data = variables;

loader.load (request); 

In B.swf, it is checking the Flashvars like so. It works fine from html side

this.loaderInfo.parameters.xml
Hartman answered 20/2, 2010 at 15:54 Comment(1)
It's an ambiguous question. There are two parts: 1) How to get FlashVars into B, which I answer below. 2) How to pass any kind of variables into B from A.Seale
O
17

Although the querystring method should work fine locally, if you're using Flash Player 10.2, there's a new API for this.

var context:LoaderContext = new LoaderContext();
context.parameters = {'xml': 'test.xml'};
loader.load(request, context);

The documentation is here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/LoaderContext.html#parameters

Orvie answered 19/3, 2011 at 21:7 Comment(3)
Seems to be removed from the API. parameters is undefined :(Chlamydospore
@Sean: my bad, targeting 10.1. sorryChlamydospore
Didn't work for me as querystring (even in 10.2), but worked great when using LoaderContext in 10.2 -- +1Electromagnetism
S
2

It's not necessary that "A" pass FlashVars to "B". Just have B access the FlashVars itself. The following will work whether B is inside of A, or top-level itself:

Add an ADDED_TO_STAGE event listener in B's constructor. e.g.:

function B(){
    this.addEventListener(Event.AddedToStage, onAddedToStageHandler);
}

When you have access to the stage you can now access the FlashVars in A this way:

To properly see a variable called myVar flashVar in B.swf, you do (inside B):

private function onAddedToStageHandler(){
var flashVars : Object = LoaderInfo(this.stage.loaderInfo).parameters;
// now you have access to your flashVars!
trace(flashVars.myVar);
}

stage.loaderInfo is what you need to look at.

Seale answered 17/10, 2011 at 16:45 Comment(0)
T
0

you can add the flash vars into the URI when you are loading it

URLRequest(String("B.swf" + "?myvar=45"));

the problem is that when you loaded the string in the uri, it is put inside an Object loaderInfo.parameters so if you want to pass those parameters, you need to create a string to pass these into.

here's a script from http://ragona.com/blog/pass-flashvars-loaded-swf/ that shows how to convert that into a string array again

//:: Store loader info
var lInfo:Object = this.root.loaderInfo.parameters;
//:: Flashvars
var fVars:String = "?whee=nada"; //:: Getting the syntax change (? --> &) out of the way with a dummy var

//:: Set path + data
for (var flashVar in lInfo)
{
    fVars += "&" + flashVar + "=" + lInfo[flashVar];
}

var myRequest:URLRequest = new URLRequest(String("/myPath.swf" + fVars));
Trigonometry answered 20/2, 2010 at 16:31 Comment(2)
Thanks, but I forgot to mention, I have tried that too and it gives the same error. I saw a page mentioned because passing FlashVars cannot work with local swf, is that the problem?Hartman
I see no reason for that not working, but you can try "sandboxing" the permissions for the folder it is in.Trigonometry
H
0

How do I sandbox the permissions? I tried adding the folder via the settings, but that didn't work too.

http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html#117502

I made a simple as new project. This is the complete code.

var mc:MovieClip = new MovieClip ();

var loader:Loader = new Loader ();
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, OnComplete);

var request:URLRequest = new URLRequest ("B.swf"); 

var variables : URLVariables = new URLVariables(); 
variables.xml = "test2.xml"; 

// This line causes the error 2044, if i comment out, it runs fine without FlashVars   
request.data = variables; 

loader.load (request);  


function OnComplete (e:Event)
{
    trace ("On Complete");
    mc = e.currentTarget.content as MovieClip;
    addChild (mc);
}
Hartman answered 22/2, 2010 at 8:37 Comment(2)
I found what's the problem. It works if I run the swf in the normal flash player, but not the debug version when I launch from editor with Ctrl+Enter. But I cannot debug anymore hmmm..Hartman
trace (Security.sandboxType), gives me localTrusted. I added Security.allowInsecureDomain("*") but still no go.Hartman

© 2022 - 2024 — McMap. All rights reserved.