as3项目要调用外部swf里的类有3种方法:
1.将外部的swf发布为swc,使用时将swc引用添加到相应的项目中,这应该是最简单的一种引用。不过当项目中的类或组件比较多时就会使项目发布生成的swf文件大小过大;
2.通过资源绑定外部的,然后直接通过类名获取。如:[Embed(source="assets/icon/skin.swf",symbol="Btn_Max")],这种方法也会引起swf文件过大;
3.通过域来来获取外部swf里的绑定类,这种方法可以在需要用时才去加载相应的swf文件然后再获取所需要的类。
下面是根据第三种方法来获取所需要的类:
View Code
1 package com.mobiano.flipbook.pageeditor 2 { 3 import com.flasht.tui.display.TArrow; 4 import com.mobiano.flipbook.config.FlipBookConfig; 5 import com.tflash.Components.util.SWFLoader; 6 7 import flash.display.DisplayObject; 8 import flash.errors.IOError; 9 import flash.events.Event; 10 import flash.events.IOErrorEvent; 11 import flash.events.ProgressEvent; 12 import flash.utils.Dictionary; 13 14 public class PlugInManager 15 { 16 public static var allExternalClass:Object={}; 17 //public var loadingQueue:Object={}; 18 //public var swfLoader:SWFLoader; 19 public var loadingQueue:Dictionary=new Dictionary(); 20 private static var canInstace:Boolean=false; 21 private static var instace:PlugInManager; 22 23 private var filePrefix:String="./files/pageConfig/"; 24 private var fileSuffix:String=".swf"; 25 public function PlugInManager() 26 { 27 if(!canInstace){ 28 throw new Error("Can't new PlugInManager"); 29 } 30 } 31 32 public static function getInstace():PlugInManager{ 33 if(instace==null){ 34 canInstace=true; 35 instace=new PlugInManager(); 36 canInstace=false; 37 } 38 39 return instace; 40 } 41 42 public function getComponent(target:TAnnoPlugIn,cpName:String,extClassName:String):void{ 43 if(cpName==null||cpName.length<1||extClassName==null||extClassName.length<1)return ; 44 if(allExternalClass.hasOwnProperty()){ 45 //return allExternalClass[cpName]; 46 var swfLoader:SWFLoader=allExternalClass[cpName]; 47 var cl:Class=swfLoader.GetClass(extClassName); 48 if(cl!=null){ 49 var extObj:IPlugInInterface=createSWFClass(cl); 50 if(extObj!=null){ 51 target.extObj=extObj; 52 } 53 } 54 }else{ 55 load(target,cpName,extClassName); 56 } 57 //return null; 58 } 59 60 public function getSwfUrl(cpName):String{ 61 if(cpName!=null){ 62 return filePrefix+cpName+fileSuffix; 63 } 64 return null; 65 } 66 67 protected function getURLFrom(url:String):String{ 68 return com.mobiano.flipbook.config.FlipBookConfig.getURLForm(url); 69 } 70 71 private function load(target:TAnnoPlugIn,cpName:String,extClName:String):void{ 72 var swfUrl:String=getSwfUrl(cpName); 73 if(swfUrl==null||swfUrl.length<1)return; 74 swfUrl=getURLFrom(swfUrl); 75 76 var swfLoader:SWFLoader=new SWFLoader(swfUrl); 77 swfLoader.addEventListener(Event.COMPLETE,onComplete); 78 swfLoader.addEventListener(ProgressEvent.PROGRESS,onProgress); 79 swfLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError); 80 var obj:Object={target:target,compontName:cpName,extClassName:extClName}; 81 82 //loadingQueue[cpName]=obj; 83 loadingQueue[swfLoader]=obj; 84 swfLoader.Load(); 85 86 } 87 88 private function onComplete(evt:Event):void{ 89 trace(evt.currentTarget); 90 if(evt.currentTarget is SWFLoader){ 91 var loader:SWFLoader=evt.currentTarget as SWFLoader; 92 if(loader in loadingQueue){ 93 var obj:Object=loadingQueue[loader]; 94 if(obj["target"]&&obj["compontName"]&&obj["extClassName"]){ 95 var cpName:String=obj["compontName"]; 96 var extClassName:String=obj["extClassName"]; 97 98 allExternalClass[cpName]=loader; 99 100 var cl:Class=loader.GetClass(extClassName); 101 var target:TAnnoPlugIn=obj["target"]; 102 if(cl!=null){ 103 //allExternalClass[cpName]=cl; 104 var extObj:IPlugInInterface=createSWFClass(cl); 105 if(extObj!=null){ 106 target.extObj=extObj; 107 } 108 } 109 } 110 //loader.GetClass( 111 //var target:TAnnoPlugIn=loadingQueue[loader]; 112 } 113 } 114 } 115 116 private function createSWFClass(cl:Class):IPlugInInterface{ 117 var extObj:IPlugInInterface; 118 try{ 119 if(cl!=null){ 120 extObj=new cl(); 121 } 122 123 }catch(e:Error){ 124 return null; 125 } 126 return extObj; 127 } 128 129 private function onProgress(evt:ProgressEvent):void{ 130 131 } 132 133 private function onIOError(evt:IOError):void{ 134 throw new Error("Load swf error:"+evt); 135 } 136 137 } 138 }
View Code
1 package com.tflash.Components.util 2 { 3 import flash.display.DisplayObject; 4 import flash.display.Loader; 5 import flash.display.LoaderInfo; 6 import flash.events.Event; 7 import flash.events.EventDispatcher; 8 import flash.events.IEventDispatcher; 9 import flash.events.IOErrorEvent; 10 import flash.events.ProgressEvent; 11 import flash.net.URLRequest; 12 import flash.system.ApplicationDomain; 13 import flash.system.LoaderContext; 14 15 [Event(name="complete", type="flash.events.Event")] 16 [Event(name="progress",type="flash.events.ProgressEvent")] 17 [Event(name="io_error",type="flash.events.IOErrorEvent")] 18 19 public class SWFLoader extends EventDispatcher 20 { 21 private var loader:Loader; 22 private var content:DisplayObject; 23 private var loadComplete:Boolean=false; 24 private var url:String; 25 public function SWFLoader(url:String) 26 { 27 this.url=url; 28 29 } 30 31 public function Load(url:String=null):void{ 32 if(url!=null){ 33 this.url=url; 34 } 35 loadComplete=false; 36 if(loader==null){ 37 loader=new Loader(); 38 }else{ 39 loader.unloadAndStop(true); 40 if(loader.contentLoaderInfo.hasEventListener(Event.COMPLETE)){ 41 loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete); 42 } 43 if(loader.contentLoaderInfo.hasEventListener(ProgressEvent.PROGRESS)){ 44 loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); 45 } 46 if(loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)){ 47 loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError) 48 } 49 } 50 51 var context:LoaderContext=new LoaderContext(); 52 context.applicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain); 53 var request:URLRequest=new URLRequest(this.url); 54 loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete); 55 loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onLoadProgress); 56 loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); 57 loader.load(request,context); 58 } 59 60 61 private function onLoadProgress(evt:ProgressEvent):void{ 62 63 this.dispatchEvent(evt); 64 } 65 66 private function onLoadComplete(evt:Event):void{ 67 evt.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete); 68 evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); 69 evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); 70 content=(evt.currentTarget as LoaderInfo).content; 71 loadComplete=true; 72 this.dispatchEvent(new Event(Event.COMPLETE)); 73 74 } 75 76 private function onLoadIOError(evt:IOErrorEvent):void{ 77 evt.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete); 78 evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); 79 evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); 80 81 this.dispatchEvent(evt); 82 } 83 84 85 /** 86 * 获取当前ApplicationDomain内的类定义 87 * 88 * name类名称,必须包含完整的命名空间,如 Grave.Function.SWFLoader 89 * info加载swf的LoadInfo,不指定则从当前域获取 90 * return获取的类定义,如果不存在返回null 91 */ 92 public function GetClass(name:String):Class{ 93 94 if(loadComplete&&loader!=null){ 95 if(loader.contentLoaderInfo.applicationDomain.hasDefinition(name)){ 96 return loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class; 97 }else{ 98 return null; 99 } 100 } 101 return null; 102 } 103 104 public function GetContent():DisplayObject{ 105 return content; 106 } 107 108 public function GetLoader():Loader{ 109 110 return loader; 111 } 112 113 } 114 }