如何运行内部的Flex应用程序外部SWF?应用程序、Flex、SWF

2023-09-08 13:15:23 作者:山有木兮木有枝

编辑:由于答案我改变code张贴。我已经添加了 的Security.allowDomain(*​​) 线,这条线将引发我一个错误。那么,怎么可能进行?

我要运行一个动作脚本3.0应用到Flex应用程序。要做到这一点,我已经做了以下内容:

I want to run an Action Script 3.0 Application into a Flex Application. To do this I've done the following:

<?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会引发我一个例外:

The problem is that in the calling of swfApplication.init(); the AIR Player throws me an exception:

安全沙箱冲突:调用者文件:///path/to/the/application.swf无法访问阶段拥有的应用程序:/SWFApplicationLoader.swf

这是因为某处 application.swf 我用的阶段是这样的:

This is because somewhere in application.swf I use the stage like this:

if (root.stage != null)
    root.stage.addEventListener(Event.REMOVED, someFunction);
root.stage.stageFocusRect = false;

我怎样才能加载这个SWF应用程序和使用阶段,没有任何问题?

How can I load this swf application and USE the stage without any problems?

推荐答案

您可以尝试加载你的 SWF 暂时成的ByteArray 键,然后将其加载到的SWFLoader

You can try to load your SWF temporarily into a ByteArray and then load it with your SWFLoader.

不要忘记设置allowLoadBytes$c$cExecution真因为你的SWF已为code内。

Don't forget to set allowLoadBytesCodeExecution to true since your SWF has as code inside.

当然,要确保你的加载SWF是为你的应用足够安全的,因为它不会在所有的属性访问。

Of course be sure that your loaded swf is secure enough for your application since it will have access at all your 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.allowLoadBytesCodeExecution=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
}