I'm trying to set up some default callbacks for listeners dynamically, and I'm not having much success at the moment.
Array:
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"));
And then:
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);
}
}
}
Suggestions?
even if you need to get anything very specific from this events imho the best choice is to the following:
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;
}
}
and about dynamical generation, if it's really the only solution:
create 2 arrays - for dispatchers and for listener functions. and one more to hold descriptions of dispatcher-listener pairs. or you may store dispatcher with listener and description in an Object and have an array of such objects, or maybe develop a specific data structure... anyway:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.