as3调用外部swf里的类的方法
生活随笔
收集整理的這篇文章主要介紹了
as3调用外部swf里的类的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
as3項目要調(diào)用外部swf里的類有3種方法:
1.將外部的swf發(fā)布為swc,使用時將swc引用添加到相應的項目中,這應該是最簡單的一種引用。不過當項目中的類或組件比較多時就會使項目發(fā)布生成的swf文件大小過大;
2.通過資源綁定外部的,然后直接通過類名獲取。如:[Embed(source="assets/icon/skin.swf",symbol="Btn_Max")],這種方法也會引起swf文件過大;
3.通過域來來獲取外部swf里的綁定類,這種方法可以在需要用時才去加載相應的swf文件然后再獲取所需要的類。
下面是根據(jù)第三種方法來獲取所需要的類:
?
package com.mobiano.flipbook.pageeditor {import com.flasht.tui.display.TArrow;import com.mobiano.flipbook.config.FlipBookConfig;import com.tflash.Components.util.SWFLoader;import flash.display.DisplayObject;import flash.errors.IOError;import flash.events.Event;import flash.events.IOErrorEvent;import flash.events.ProgressEvent;import flash.utils.Dictionary;public class PlugInManager{public static var allExternalClass:Object={};//public var loadingQueue:Object={};//public var swfLoader:SWFLoader;public var loadingQueue:Dictionary=new Dictionary();private static var canInstace:Boolean=false;private static var instace:PlugInManager;private var filePrefix:String="./files/pageConfig/";private var fileSuffix:String=".swf";public function PlugInManager(){if(!canInstace){throw new Error("Can't new PlugInManager"); }}public static function getInstace():PlugInManager{if(instace==null){canInstace=true;instace=new PlugInManager();canInstace=false;}return instace;} public function getComponent(target:TAnnoPlugIn,cpName:String,extClassName:String):void{if(cpName==null||cpName.length<1||extClassName==null||extClassName.length<1)return ;if(allExternalClass.hasOwnProperty()){//return allExternalClass[cpName];var swfLoader:SWFLoader=allExternalClass[cpName];var cl:Class=swfLoader.GetClass(extClassName);if(cl!=null){var extObj:IPlugInInterface=createSWFClass(cl);if(extObj!=null){target.extObj=extObj;}}}else{load(target,cpName,extClassName);}//return null;}public function getSwfUrl(cpName):String{if(cpName!=null){return filePrefix+cpName+fileSuffix;}return null;}protected function getURLFrom(url:String):String{return com.mobiano.flipbook.config.FlipBookConfig.getURLForm(url);}private function load(target:TAnnoPlugIn,cpName:String,extClName:String):void{var swfUrl:String=getSwfUrl(cpName);if(swfUrl==null||swfUrl.length<1)return;swfUrl=getURLFrom(swfUrl);var swfLoader:SWFLoader=new SWFLoader(swfUrl);swfLoader.addEventListener(Event.COMPLETE,onComplete);swfLoader.addEventListener(ProgressEvent.PROGRESS,onProgress);swfLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);var obj:Object={target:target,compontName:cpName,extClassName:extClName};//loadingQueue[cpName]=obj;loadingQueue[swfLoader]=obj;swfLoader.Load();}private function onComplete(evt:Event):void{trace(evt.currentTarget);if(evt.currentTarget is SWFLoader){var loader:SWFLoader=evt.currentTarget as SWFLoader;if(loader in loadingQueue){var obj:Object=loadingQueue[loader];if(obj["target"]&&obj["compontName"]&&obj["extClassName"]){var cpName:String=obj["compontName"];var extClassName:String=obj["extClassName"];allExternalClass[cpName]=loader;var cl:Class=loader.GetClass(extClassName);var target:TAnnoPlugIn=obj["target"];if(cl!=null){//allExternalClass[cpName]=cl;var extObj:IPlugInInterface=createSWFClass(cl);if(extObj!=null){target.extObj=extObj;}}}//loader.GetClass(//var target:TAnnoPlugIn=loadingQueue[loader];} }}private function createSWFClass(cl:Class):IPlugInInterface{var extObj:IPlugInInterface;try{if(cl!=null){extObj=new cl();}}catch(e:Error){return null;}return extObj;}private function onProgress(evt:ProgressEvent):void{}private function onIOError(evt:IOError):void{throw new Error("Load swf error:"+evt);}} } package com.tflash.Components.util {import flash.display.DisplayObject;import flash.display.Loader;import flash.display.LoaderInfo;import flash.events.Event;import flash.events.EventDispatcher;import flash.events.IEventDispatcher;import flash.events.IOErrorEvent;import flash.events.ProgressEvent;import flash.net.URLRequest;import flash.system.ApplicationDomain;import flash.system.LoaderContext;[Event(name="complete", type="flash.events.Event")][Event(name="progress",type="flash.events.ProgressEvent")][Event(name="io_error",type="flash.events.IOErrorEvent")]public class SWFLoader extends EventDispatcher{private var loader:Loader;private var content:DisplayObject;private var loadComplete:Boolean=false;private var url:String;public function SWFLoader(url:String){this.url=url;}public function Load(url:String=null):void{if(url!=null){this.url=url;}loadComplete=false;if(loader==null){loader=new Loader();}else{loader.unloadAndStop(true);if(loader.contentLoaderInfo.hasEventListener(Event.COMPLETE)){loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete);}if(loader.contentLoaderInfo.hasEventListener(ProgressEvent.PROGRESS)){loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);}if(loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)){loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError)}}var context:LoaderContext=new LoaderContext();context.applicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain);var request:URLRequest=new URLRequest(this.url);loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onLoadProgress);loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);loader.load(request,context);}private function onLoadProgress(evt:ProgressEvent):void{this.dispatchEvent(evt);}private function onLoadComplete(evt:Event):void{evt.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete);evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);content=(evt.currentTarget as LoaderInfo).content;loadComplete=true;this.dispatchEvent(new Event(Event.COMPLETE));}private function onLoadIOError(evt:IOErrorEvent):void{evt.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete);evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);this.dispatchEvent(evt);}/*** 獲取當前ApplicationDomain內(nèi)的類定義** name類名稱,必須包含完整的命名空間,如 Grave.Function.SWFLoader* info加載swf的LoadInfo,不指定則從當前域獲取* return獲取的類定義,如果不存在返回null*/public function GetClass(name:String):Class{if(loadComplete&&loader!=null){if(loader.contentLoaderInfo.applicationDomain.hasDefinition(name)){return loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;}else{return null;}}return null;}public function GetContent():DisplayObject{return content;}public function GetLoader():Loader{return loader;}} }轉(zhuǎn)載于:https://www.cnblogs.com/zhepama/p/3467153.html
總結(jié)
以上是生活随笔為你收集整理的as3调用外部swf里的类的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中装箱与拆箱
- 下一篇: Thinkphp5.0 多图上传名称重复