在AS3中动态应用函数

时间:2010-11-03 21:05:01

标签: actionscript-3 function dynamic flash-cs4

我正在尝试动态地为侦听器设置一些默认回调,目前我没有取得多大成功。

阵列:

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 个答案:

答案 0 :(得分: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个数组 - 用于调度程序和侦听器函数。还有一个用于保存调度员 - 听众对的描述。或者你可以在一个Object中存储带有监听器和描述的调度程序,并有一组这样的对象,或者可能开发一个特定的数据结构...... 无论如何:

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);
}