flash瘦身技巧-动态加载swc

程序需要额外的第三方库,如:A.swf和B.swf都需要一样的第三方裤(C.swc),但flash编译后A.swf和B.swf都完全包含了C.swc,增加了swf文件大小。动态加载可以给swf瘦身。

swc库文件其实是个压缩文档,把swc改成zip文件可以看到里面包含了library.swf和catalog.xml两个文件。(完全可以把library.swf解压出来像加载普通swf文件一样处理)

程序需要用到FZip第三方库,要编译通过在这里下载。http://codeazur.com.br/lab/fzip/

 1 package 
 2 {
 3     import deng.fzip.FZip;
 4     import deng.fzip.FZipFile;
 5     import flash.display.Loader;
 6     import flash.display.Sprite;
 7     import flash.events.Event;
 8     import flash.net.URLRequest;
 9     import flash.system.ApplicationDomain;
10     import flash.system.LoaderContext;
11     import flash.utils.getDefinitionByName;
12 
13     public class Main extends Sprite 
14     {
15         
16         public function Main():void 
17         {
18             if (stage) init();
19             else addEventListener(Event.ADDED_TO_STAGE, init);
20         }
21         
22         private function init(e:Event = null):void 
23         {
24             removeEventListener(Event.ADDED_TO_STAGE, init);
25             start();
26         }
27         
28         private function start():void
29         {
30             var zip:FZip = new FZip();
31             zip.addEventListener(Event.COMPLETE, onLoadSWC);
32             zip.load(new URLRequest("../lib/lib.swc"));
33         }
34         
35         private function onLoadSWC(e:Event):void
36         {
37             var zip:FZip = e.target as FZip;
38             var zipFile:FZipFile = zip.getFileByName("library.swf");
39             var loader:Loader = new Loader();
40             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
41             loader.loadBytes(zipFile.content, new LoaderContext(false, ApplicationDomain.currentDomain, null));
42         }
43         
44         private function onLoad(e:Event):void
45         {
46             var mcCls:Class = getDefinitionByName("swcMC") as Class;
47             addChild(new mcCls());
48         }
49     }
50     
51 }

 

posted @ 2013-02-18 22:50  liusijian  阅读(623)  评论(0编辑  收藏  举报