例子1:
圖2
這是一個(gè)虛擬人物形象的動(dòng)作包,其中包含了8種不同的動(dòng)作 在使用ClassLoader加載這個(gè)swf的動(dòng)作包后,即可使用getClass來(lái)調(diào)用這些素材,而且可以重復(fù)的new這些元件,來(lái)達(dá)到多次重復(fù)使用
import index.base.net.ClassLoader;
var cl:ClassLoader = new ClassLoader; cl.load("main.swf");
cl.addEventListener(Event.COMPLETE,fun);
function fun(e:Event){ var tmp = cl.getClass("drag"); addChild(new tmp); }
例子2: 將設(shè)我有一個(gè)類庫(kù),有這么三個(gè)類
import index.base.net.ByteLoader; import index.base.net.ClassLoader; import index.base.geom.Dot;
var bl:ByteLoader; var cl:ClassLoader; var dot:Dot;
然后把它編譯成swf
我們另外建一個(gè)文件,來(lái)加載這個(gè)所謂的類庫(kù)
import index.base.net.ClassLoader;
var cl:ClassLoader = new ClassLoader; cl.load("main.swf");
cl.addEventListener(Event.COMPLETE,fun);
function fun(e:Event){ var tmp1 = cl.getClass("index.base.net.ByteLoader"); trace(tmp1) var tmp2 = cl.getClass("index.base.net.ClassLoader"); trace(tmp2) var tmp3 = cl.getClass("index.base.geom.Dot"); trace(tmp3) }
/** * trace的結(jié)果: * [class ByteLoader] * [class ClassLoader] * [class Dot] */
我們的目的就達(dá)到了!
接下來(lái)是源代碼!
package index.base.net{ import flash.display.Loader; import flash.net.URLRequest; import flash.utils.ByteArray; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.EventDispatcher; import flash.system.LoaderContext; public class ClassLoader extends EventDispatcher{ public var url:String; public var loader:Loader; //構(gòu)造函數(shù) public function ClassLoader(obj:Object = null,lc:LoaderContext = null) { if(obj != null){ if(obj is ByteArray){ loadBytes(obj as ByteArray,lc); }else if(obj is String){ load(obj as String,lc); }else{ throw new Error("參數(shù)錯(cuò)誤,構(gòu)造函數(shù)第一參數(shù)只接受ByteArray或String"); } } } //加載 public function load(_url:String,lc:LoaderContext = null):void{ url = _url; loader = new Loader; loader.load(new URLRequest(url),lc); addEvent(); } //加載字節(jié) public function loadBytes(bytes:ByteArray,lc:LoaderContext = null):void{ loader = new Loader; loader.loadBytes(bytes,lc); addEvent(); } //開(kāi)始偵聽(tīng) private function addEvent():void{ loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFun); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFun); } //結(jié)束偵聽(tīng) private function delEvent():void{ loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressFun); loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeFun); } //加載成功,發(fā)布成功事件 private function completeFun(e:Event):void { delEvent(); dispatchEvent(e); } //加載過(guò)程 private function progressFun(e:ProgressEvent):void{ dispatchEvent(e); } //獲取定義 public function getClass(className:String):Object { return loader.contentLoaderInfo.applicationDomain.getDefinition(className); } //是否含有該定義 public function hasClass(className:String):Boolean { return loader.contentLoaderInfo.applicationDomain.hasDefinition(className); } //清除 public function clear():void{ loader.unload(); loader = null; } } }
經(jīng)典論壇交流: http://bbs.blueidea.com/thread-2843169-1-1.html
本文鏈接:http://m.95time.cn/tech/multimedia/2008/5842.asp
出處:藍(lán)色理想
責(zé)任編輯:bluehearts
上一頁(yè) ClassLoader類 [1] 下一頁(yè)
◎進(jìn)入論壇RIA設(shè)計(jì)與應(yīng)用版塊參加討論
|