http://bbs.9ria.com/thread-66207-1-1.html
以下便是用 addFrameScript 方式動態在外部 SWF 最後一格加上卸載的命令
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:layout>
<s:VerticalLayout verticalAlign="middle" horizontalAlign="center" />
</s:layout>
<mx:SWFLoader id="swfLdr" minWidth="100" minHeight="100">
<mx:complete>
<![CDATA[
var mc:MovieClip = swfLdr.content as MovieClip;
if (mc) {
mc.addFrameScript(mc.totalFrames - 1, swfLdr.unloadAndStop);
}
]]>
</mx:complete>
</mx:SWFLoader>
<s:HGroup>
<s:Button label="Load Anim 01" click="swfLdr.source = 'Anim01.swf';" />
<s:Button label="Load Anim 02" click="swfLdr.source = 'Anim02.swf';" />
<s:Button label="Load Anim 03" click="swfLdr.source = 'Anim03.swf';" />
</s:HGroup>
</s:Application>
實際用不同版本 SWF 測試時
會發現到以上的方式只對 AS3 版的 SWF 有效即便外部 SWF 內沒有任何程式
遇到了 AS1, 2 的 SWF,content 類別就會變成了 AVM1Movie
不能使用 addFrameScript,也沒有任何影格事件或屬性可以用~
後來想到最近出現的一個 AS3 Open Source Lib. - as3swf
它可以用 AS3 動態操作 SWF Binary 資料
可以動態修改 SWF Metadata、Atributes、影格資料、點陣圖資料....
不過我只需要用到它 0.1% 的功能而已 - 將 SWF 改為 AS3 版本
稍微修改上面程式,利用 as3swf 修改外部 SWF AS 版本
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:layout>
<s:VerticalLayout verticalAlign="middle" horizontalAlign="center" />
</s:layout>
<mx:SWFLoader id="swfLdr" minWidth="100" minHeight="100">
<mx:complete>
<![CDATA[
import com.codeazur.as3swf.SWF;
import com.codeazur.as3swf.tags.TagFileAttributes;
if (swfLdr.content is MovieClip) {
var mc:MovieClip = swfLdr.content as MovieClip;
mc.addFrameScript(mc.totalFrames - 1, swfLdr.unloadAndStop);
} else if (swfLdr.content is AVM1Movie) {
var swf:SWF = new SWF(swfLdr.content.loaderInfo.bytes);
var tagFile:TagFileAttributes = swf.tags[0] as TagFileAttributes;
tagFile.actionscript3 = true;
var newByte:ByteArray = new ByteArray();
swf.publish(newByte);
callLater(swfLdr.load, [newByte]);
}
]]>
</mx:complete>
</mx:SWFLoader>
<s:HGroup>
<s:Button label="Load Anim 01" click="swfLdr.source = 'Anim01.swf';" />
<s:Button label="Load Anim 02" click="swfLdr.source = 'Anim02.swf';" />
<s:Button label="Load Anim 03" click="swfLdr.source = 'Anim02.swf';" />
</s:HGroup>
</s:Application>
---------------------------------------------------------------------
尝试结果:
Flex项目工程中,报错:
callLater(swfLdr.load, [newByte]);
SecurityError: Error #3226: Cannot import a SWF file when LoaderContext.allowCodeImport is false.
修改如下也是同样的错误:
swfLdr.loaderContext.allowCodeImport = true;
swfLdr.loaderContext.applicationDomain = ApplicationDomain.currentDomain;
swfLdr.loaderContext.allowLoadBytesCodeExecution = true;
trace("allowCodeImport: "+swfLdr.loaderContext.allowCodeImport);
callLater(swfLdr.load, [newByte]);
还来改用as手机工程,貌似无法使用SWFLoader和callLater(),修改如下:
private var swfLoader:Loader = new Loader();
private function loadComplete(e:Event):void {
trace("loadComplete()");
if (swfLoader.content is MovieClip) {
trace(swfLoader.loaderInfo.url + "is as3 MovieClip");
var mc:MovieClip = swfLoader.content as MovieClip;
mc.addFrameScript(mc.totalFrames - 1, swfLoader.unloadAndStop);
} else if (swfLoader.content is AVM1Movie) {
trace(swfLoader.loaderInfo.url + "is as2 MovieClip");
var swf:SWF = new SWF(swfLoader.content.loaderInfo.bytes);
var tagFile:TagFileAttributes = swf.tags[0] as TagFileAttributes;
tagFile.actionscript3 = true;
var newByte:ByteArray = new ByteArray();
swf.publish(newByte);
var lc:LoaderContext = new LoaderContext();
lc.allowCodeImport = true;
swfLoader.loadBytes(newByte, lc);
}
}
不会报错了,但是加载进入了的swf中的as还是无法运行,也无法自动卸载