我正在使用HTTP请求以二进制格式下载图像。下载完成后,我想处理它,但我也想将图像的ID传递给完整的处理函数......这是怎么做的?
var loader:URLLoader = new URLLoader();
for(var i:int = 0 ; i<5; i++){
/* When completed I want to access the variable "i" */
loader.addEventListener(Event.complete, completeHandler);
loader.load(/* a url request */);
}
private function completeHandler(event:Event):void
{
/* I want to access the passed parameter "i" so
it is the same as it was when the eventListener was added, 0,1,2,3 or 4 */
}
这可能吗?我已尝试扩展Event,但我想处理 COMPLETE 事件
由于 菲尔
答案 0 :(得分:4)
这应该可以使用Flex的动态功能构造。提出了类似的问题here和here。
以下是一个例子:
参数和处理程序:
var parameters:String = "Some parameter I want to pass";
private function loadLocalData(e:Event, parameter:String):void
{
// voila, here's your parameter
}
private function addArguments(method:Function, additionalArguments:Array):Function
{
return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));}
}
您的示例中的用法:
for(var i:int = 0 ; i<5; i++){
/* When completed I want to access the variable "i" */
loader.addEventListener(Event.complete, addArguments(completeHandler, [i]));
loader.load(/* a url request */);
}
private function completeHandler(event:Event, id:int):void
{
/* I want to access the passed parameter "i" so
it is the same as it was when the eventListener was added, 0,1,2,3 or 4 */
}
答案 1 :(得分:0)
exacly - 附加方法addArguments(...)是最好的解决方案,我使用同样但它调用passParameters
public function passParameters(method:Function,additionalArguments:Array):Function
{return function(event:Event):void{
method.apply(null, [event].concat(additionalArguments));}
}
这里的解释就是 - 简单而且始终有效http://sinfinity.pl/blog/2012/03/28/adding-parameters-to-event-listener-in-flex-air-as3/