AS3自寫類整理筆記 ClassLoader類第2/2頁
例子1:
圖2
這是一個(gè)虛擬人物形象的動(dòng)作包,其中包含了8種不同的動(dòng)作
在使用ClassLoader加載這個(gè)swf的動(dòng)作包后,即可使用getClass來調(diào)用這些素材,而且可以重復(fù)的new這些元件,來達(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è)類庫,有這么三個(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è)文件,來加載這個(gè)所謂的類庫
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á)到了!
接下來是源代碼!
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();
}
//開始偵聽
private function addEvent():void{
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFun);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFun);
}
//結(jié)束偵聽
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);
}
//加載過程
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;
}
}
}
相關(guān)文章
Google Analytics在Flash cs3下的使用教程分析
因?yàn)楣ぷ鞯脑?,最近使用到Google Analytics組件,這個(gè)組件在網(wǎng)上的資料很多,但是大部分都是詳談組件的優(yōu)勢(shì)的,具體的使用沒有很詳細(xì)的說明2009-02-02as3+xml+asp+access做的有獎(jiǎng)問答
as3+xml+asp+access做的有獎(jiǎng)問答實(shí)現(xiàn)代碼2009-02-02火山動(dòng)態(tài)文本滾動(dòng)條V5[AS3版]
功能說明:本版滾動(dòng)條除了繼續(xù)保持體積小(小于2K),界面容易修改,資源占用率小的優(yōu)勢(shì)外,主要有以下幾點(diǎn)改進(jìn): 1,使用AS3編寫。 2,寬高動(dòng)態(tài)指定。 3,增加滾動(dòng)條背景點(diǎn)擊事件。 4,消除了鼠標(biāo)滾輪無法同時(shí)準(zhǔn)確控制多個(gè)文本框的重大BUG。2008-03-03Actionscript 3.0中Singleton實(shí)現(xiàn) 修正篇
說明:上一篇"一個(gè)簡(jiǎn)單的Actionscript的單態(tài)模式類"的實(shí)現(xiàn)在Actionscript中報(bào)錯(cuò),具體原因會(huì)在這篇Blog中詳細(xì)說明。2009-02-02AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問題
AS3 navigateToURL導(dǎo)致ExternalInterface 執(zhí)行失敗問題2009-02-02