如何在Flex应用程序中运行外部SWF?

Luc*_*hez 5 air flash actionscript-3 flex4

编辑:由于答案我更改了发布的代码.我已经添加了该 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>
Run Code Online (Sandbox Code Playgroud)

问题是在调用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;
Run Code Online (Sandbox Code Playgroud)

如何加载此swf应用程序并使用舞台没有任何问题?

Pat*_*ick 18

你可以尝试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
}
Run Code Online (Sandbox Code Playgroud)