编辑:由于答案我更改了发布的代码.我添加了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 Player抛出了一个异常:
安全沙箱冲突:调用者文件:///path/to/the/application.swf无法访问app所拥有的阶段:/SWFApplicationLoader.swf.
这是因为在application.swf的某个地方我使用这样的舞台:
if (root.stage != null) root.stage.addEventListener(Event.REMOVED,someFunction); root.stage.stageFocusRect = false;
如何加载此swf应用程序并使用舞台没有任何问题?
解决方法
您可以尝试将SWF临时加载到ByteArray中,然后使用SWFLoader加载它.
不要忘记将allowCodeImport
设置为true,因为您的SWF中包含代码.
当然,请确保您加载的swf对您的应用程序足够安全,因为它可以访问您的所有属性.
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 }