“链”等一系列功能一起在ActionScript 3 [英] 'chain' a series of functions together in actionscript 3
问题描述
我调用一个函数,并添加一个监听器,当函数返回的一些数据。当返回的数据,我需要调用另一个函数等。
I'm calling a function and adding a listener for when the function returns some data. when the data is returned i need to call another function and so on.
有没有一种简单的方法,以链,这些功能结合在一起,使第一个火 - 等待监听器,然后触发第二个创建一个监听者,并依此类推,直至最后一个调用定义一个单独的函数在开始时。它将我想工作,对同一线作为大容量装载器脚本。
Is there an easy way to 'chain' these functions together so that the first one fires - waits for the listener then fires the second one creating a listener for that and so on until the last one calls a separate function that is defined at the start. It would i suppose work on the same lines as the bulk loader scripts.
我设想的code工作是这样的:
I'm envisaging the code working something like this:
var dataLoader:DataLoader = new DataLoader(onAllComplete, onError);
dataLoader.add(getData1, {args1, args2}, listnerType.DATA_LOADED);
dataLoader.add(getData2, {args3, args4}, listnerType.DATA_LOADED);
dataLoader.add(getData3, {args5, args6}, listnerType.DATA_LOADED);
dataLoader.start();
private function onAllComplete(e:Array):void {
//where e contains an array of all the event results
}
private function onError(e:Event):void {
//will fire if there are any errors along the way
}
谢谢, 约什
Thanks, Josh
推荐答案
我也只是做一些简单的,如:(也是,这是八九不离十伪code,你需要正确的错误事件之类的东西)
I would just do something simple like: (also, this is sorta psuedo code, you'll need the correct error events and stuff)
var numLoaded:int = 0;
var numError:int = 0;
var loadingIndex:int = 0;
var itemsToLoad:Array = ['img1.jpg', 'img2.jpg', 'img3.jpg'];
public function startLoading():void{
loader.load(itemsToLoad[loadingIndex];
loader.addEventListener(Event.COMPLETE, completeHandler);
}
public function completeHandler(event:Event):void{
loadingIndex++;
numLoaded++;
if(numLoaded + numError >= itemsToLoad.length){
onAllItemsComplete();
}else{
loader.load(itemsToLoad[loadingIndex];
}
}
public function errorHandler(event:Event):void{
loadingIndex++;
numError++;
if(numLoaded + numError >= itemsToLoad.length){
onAllItemsComplete();
}else{
loader.load(itemsToLoad[loadingIndex];
}
}
这篇关于“链”等一系列功能一起在ActionScript 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!