Actionscript 3 如何在Flex应用程序中运行外部SWF?
编辑:由于答案,我更改了发布的代码。我添加了Actionscript 3 如何在Flex应用程序中运行外部SWF?,actionscript-3,flash,air,flex4,Actionscript 3,Flash,Air,Flex4,编辑:由于答案,我更改了发布的代码。我添加了Security.allowDomain(“*”)行,该行抛出了一个错误。那么,如何做到这一点呢? 我想将动作脚本3.0应用程序运行到Flex应用程序中。为此,我做了以下工作: <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication windowComplete="loadSwfApplication()" xmlns:mx="http://www.adobe.co
Security.allowDomain(“*”)
行,该行抛出了一个错误。那么,如何做到这一点呢?
我想将动作脚本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>
如何加载此swf应用程序并使用stage而不出现任何问题?如果从不同的域加载,则必须添加安全异常-
如果它在本地运行,您可能需要将它们添加到设置管理器中的受信任文件或文件夹列表中-假设外部SWF也在应用程序目录中,您可以尝试使用
应用程序:/
方案加载它:
var urlRequest:URLRequest = new URLRequest("app:/path/application.swf");
这可能会将其置于与主应用程序相同的安全上下文中。您可以尝试将
SWF
临时加载到ByteArray
中,然后使用SWFLoader
加载它
不要忘记设置为true,因为SWF中有as代码
当然,请确保您加载的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
}
你可能要考虑的一件事是,如果你试图在你的应用程序目录中运行一个SWF,那么空气会限制文件的执行。如果将文件复制到临时文件并运行它(连同
allowLoadBytesCodeExecution
设置为true
),则它可以工作
var file:File = File.applicationDirectory.resolvePath("myFile.swf");
this.tmpFile = File.createTempDirectory().resolvePath("myFile.swf");
file.copyTo(this.tmpFile);
imagePreview.loaderContext = lc;
imagePreview.source = tmpFile.url;
它不适用于Flex投影仪 只有我们使用SWFLoader和LocalConnection,因为它们可以在外部swf和主swf之间通信。谢谢你的支持 你能读懂我的教程吗 它比MovieClip或对象调用者要好得多 感谢解决方案:)
致以最诚挚的问候,Jens当我尝试添加安全异常时,它会抛出一个错误:
SecurityError:error#3207:应用程序沙盒内容无法访问此功能。在flash.system::Security$/allowDomain()
中,它已经添加到设置管理器中的受信任文件中。这在移动平台上也有效吗?如果是的话,你可以为iOS写一个Flash播放器。你知道为什么它只适用于字节数组而不适用于swfloader组件吗?
var file:File = File.applicationDirectory.resolvePath("myFile.swf");
this.tmpFile = File.createTempDirectory().resolvePath("myFile.swf");
file.copyTo(this.tmpFile);
imagePreview.loaderContext = lc;
imagePreview.source = tmpFile.url;