编辑:由于答案,我更改了发布的代码。我添加了 Security.allowDomain("*")
线,那条线给我一个错误。那么,怎样才能做到呢?
我想将 Action Script 3.0 应用程序运行到 Flex 应用程序中。为此,我执行了以下操作:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication windowComplete="loadSwfApplication()" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function loadSwfApplication()
{
// The next line throws me an error.
Security.allowDomain("*");
var urlRequest:URLRequest = new URLRequest("path/to/the/application.swf");
swfLoader.addEventListener(Event.COMPLETE, loadComplete);
swfLoader.load(urlRequest);
}
private function loadComplete(completeEvent:Event)
{
var swfApplication:* = completeEvent.target.content;
swfApplication.init(); // this is a Function that I made it in the Root class of swfApplication
}
]]>
</mx:Script>
<mx:SWFLoader id="sfwLoader"/>
</mx:WindowedApplication>
问题是在
swfApplication.init();
的调用中AIR 播放器向我抛出异常:安全沙箱违规:调用方文件:///path/to/the/application.swf 无法访问 app:/SWFApplicationLoader.swf 拥有的 Stage。
这是因为在
application.swf
的某个地方我像这样使用舞台:if (root.stage != null)
root.stage.addEventListener(Event.REMOVED, someFunction);
root.stage.stageFocusRect = false;
如何加载这个 swf 应用程序并使用舞台而不会出现任何问题?
最佳答案
您可以尝试加载您的 SWF
暂时进了ByteArray
然后用您的 SWFLoader
加载它.
不要忘记设置 allowCodeImport
为 true,因为您的 SWF 里面有作为代码。
当然,请确保您加载的 swf 对您的应用程序来说足够安全,因为它可以访问您的所有属性(property)。
private function loadSwfApplication():void {
// load the file with URLLoader into a bytearray
var loader:URLLoader=new URLLoader();
// binary format since it a SWF
loader.dataFormat=URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onSWFLoaded);
//load the file
loader.load(new URLRequest("path/to/the/application.swf"));
}
private function onSWFLoaded(e:Event):void {
// remove the event
var loader:URLLoader=URLLoader(e.target);
loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
// add an Application context and allow bytecode execution
var context:LoaderContext=new LoaderContext();
context.allowCodeImport=true;
// set the new context on SWFLoader
sfwLoader.loaderContext = context;
sfwLoader.addEventListener(Event.COMPLETE, loadComplete);
// load the data from the bytearray
sfwLoader.load(loader.data);
}
// your load complete function
private function loadComplete(completeEvent:Event):void {
var swfApplication:* = completeEvent.target.content;
swfApplication.init(); // this is a Function that I made it in the Root
// class of swfApplication
}
关于actionscript-3 - 如何在 Flex 应用程序中运行外部 SWF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2713865/