简体   繁体   English

AS3将FlashVars传递给加载的swf

[英]AS3 Pass FlashVars to loaded swf

I have a A.swf which loads B.swf onto a movieclip and needs to pass it some FlashVars. 我有一个A.swf,它将B.swf加载到一个movieclip上,需要传递一些FlashVars。 When loading B.swf with html, I can pass FlashVars fine. 使用html加载B.swf时,我可以正常传递FlashVars。 When passing from A.swf, it gets a 当从A.swf传递时,它得到一个

Error #2044: Unhandled ioError:. 错误#2044:未处理的ioError: text=Error #2032: Stream Error. text =错误#2032:流错误。 URL: file: 网址:文件:

The code in A.swf is A.swf中的代码是

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. 在B.swf中,它正在检查Flashvars。 It works fine from html side 从HTML方面它工作正常

this.loaderInfo.parameters.xml

Although the querystring method should work fine locally, if you're using Flash Player 10.2, there's a new API for this. 尽管查询字符串方法在本地工作正常,但如果您使用的是Flash Player 10.2,则会有一个新的API。

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 文档在这里: http//help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/LoaderContext.html#parameters

It's not necessary that "A" pass FlashVars to "B". “A”没有必要将FlashVars传递给“B”。 Just have B access the FlashVars itself. 让B访问FlashVars本身。 The following will work whether B is inside of A, or top-level itself: 无论B在A内部还是顶层本身,以下内容都将起作用:

Add an ADDED_TO_STAGE event listener in B's constructor. 在B的构造函数中添加ADDED_TO_STAGE事件侦听器。 eg: 例如:

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

When you have access to the stage you can now access the FlashVars in A this way: 当您有权访问舞台时,您现在可以通过以下方式访问FlashVars:

To properly see a variable called myVar flashVar in B.swf, you do (inside B): 要在B.swf中正确地看到一个名为myVar flashVar的变量,你可以(在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. stage.loaderInfo是你需要看的。

you can add the flash vars into the URI when you are loading it 您可以在加载时将flash变量添加到URI中

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. 问题是当你在uri中加载字符串时,它被放在一个Object loaderInfo.parameters所以如果你想传递这些参数,你需要创建一个字符串来传递它们。

here's a script from http://ragona.com/blog/pass-flashvars-loaded-swf/ that shows how to convert that into a string array again 这是来自http://ragona.com/blog/pass-flashvars-loaded-swf/的脚本,它显示了如何将其转换为字符串数组

//:: 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));

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 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM