加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

actionscript-3 – 如何在Flex应用程序中运行外部SWF?

发布时间:2020-12-15 02:10:54 所属栏目:百科 来源:网络整理
导读:编辑:由于答案我更改了发布的代码.我添加了Security.allowDomain(“*”)行,该行会引发错误.那么,怎么做呢? 我想将Action Script 3.0应用程序运行到Flex应用程序中.为此,我做了以下事情: ?xml version="1.0" encoding="utf-8"?mx:WindowedApplication wind
编辑:由于答案我更改了发布的代码.我添加了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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读