在AS3

2010-11-03 40 views
0

我試圖動態地建立了聽衆一些默認的回調,而我並沒有在此刻很大的成功動態應用的功能。在AS3

陣:

var URLLoader_SharedCallbacks:Array = new Array(); 
URLLoader_SharedCallbacks.push(new Array (HTTPStatusEvent, HTTPStatusEvent.HTTP_STATUS, "URLLoader_HTTPStatus")); 
URLLoader_SharedCallbacks.push(new Array (IOErrorEvent, IOErrorEvent.IO_ERROR, "URLLoader_IOError")); 
URLLoader_SharedCallbacks.push(new Array (Event, Event.OPEN, "URLLoader_Open")); 

然後:

function URLLoader_SharedCallbacks_Add(ul:URLLoader):void 
{ 
    for each(var arr:Array in this.URLLoader_SharedCallbacks) 
    { 
     var fnc:Function = function(evt:arr[0]) 
     { 
      trace("evt = " + evt) 
     } 
     if(!this[ul].hasEventListener(arr[2])) 
     { 
      this[ul].addEventListener(fnc); 
     } 
    } 
} 

建議?

回答

1

即使你需要得到任何東西,從這個事件非常具體恕我直言,最好的選擇是以下幾點:

private function handler(e:Event): void{ 
    switch(e.type){ 
     case IOErrorEvent.IO_ERROR: 
     //treat it like IOErrorEvent 
     break; 
     case Event.CLOSE: 
     //treat it like Event.CLOSE 
     break; 
     case HTTPStatusEvent.HTTP_STATUS: 
     //treat it like HTTPStatusEvent 
     break; 
    } 
} 

和有關動態生成,如果它真的是唯一的解決辦法:

創建2數組 - 用於調度程序和偵聽器函數。還有一個用於保存調度員 - 監聽者對的描述。或者你可以存儲與聽衆和描述調度員對象,並有這樣的對象的數組,或者可能制定一項具體的數據結構... 反正:

private var funcArray: Array = new Array(); 
private var loaderArray: Array = new Array(); 
private var infoArray: Array = new Array(); 
private function createListener():Function { 
    var fn:Function = function(e:Event): void { switch((e as Event).type) { case IOErrorEvent.IO_ERROR: /*treat it like IOErrorEvent*/ break; case Event.CLOSE:/*treat it like Event.CLOSE*/ break; case HTTPStatusEvent.HTTP_STATUS:  /*treat it like HTTPStatusEvent*/ break; }}; 
    return fn; 
} 

private function createURLLoader(url: String, description: String = 'i never care'):void{ 
    var urlo:URLLoader = new URLLoader(); 
    var fun: Function = createListener(); 
    urlo.addEventListener(IOErrorEvent.IO_ERROR, fun); 
    urlo.addEventListener(Event.CLOSE, fun); 
    urlo.addEventListener(HTTPStatusEvent.HTTP_STATUS, fun); 
    var info: Object = { 'url' : url , 'description' : description); 
    urlo.load(new URLRequest(info['url'])) 
    funcArray.push(fun); 
    loaderArray.push(urlo); 
    infoArray.push(info);//mention that arrays have always equal length and dispatcher, listener and descrition are stored under the same index 
} 
/*when data is loaded/loading timeout is over/loading failed: we need to be careful 
killing our anonimous vars: */ 

private function killEmAll(i:int):void{//i is the index for arrays 
    (loaderArray[i] as URLLoader).removeEventListener(IOErrorEvent.IO_ERROR, (funcArray[i] as Function)); 
    (loaderArray[i] as URLLoader).removeEventListener(Event.CLOSE, (funcArray[i] as Function)); 
    (loaderArray[i] as URLLoader).removeEventListener(HTTPStatusEvent.HTTP_STATUS, (funcArray[i] as Function)); 
    (loaderArray[i] as URLLoader).close();//just to be sure ;) 
    loaderArray.splice(i, 1); 
    funcArray.splice(i, 1); 
    infoArray.splice(i, 1); 
} 
+0

動態將是理想的,但在尋找你的以上的解決方案,似乎最終它幾乎與簡單地說「長路」一樣冗長。我可以肯定看到上面的代碼以備後用一些可能的用途,所以感謝您的:) – Eric 2010-11-04 13:52:02

+0

最終我做了一個函數,做了重複的工作,然後叫從每個回調函數,並通過應用的addEventListener個別回調函數。這將代碼削減了很多。再次感謝您的幫助,ww0z0k! – Eric 2010-11-04 13:53:47