2012-04-19 107 views
0

我已經填充了一個數組。AS3 dispatchEvent in forEach

我需要採取該數組中的每個項目,對該項目執行一些計算將結果推送到數組,然後移動到數組中的下一個項目,依此類推。

計算是在一個單獨的類中進行的。然後當計算完成時,我派遣一個事件並聽課以完成。

我正在使用forEach,但我需要暫停forEach函數並等待dispatchEvent偵聽器繼續,但我似乎無法得到它的工作。

跟蹤似乎表明,forEach只是運行數組並重新設置計算類。

這是我的代碼jist。我從我的服務器上的sql表填充數組,然後啓動forEach。

任何人都可以提出一個解決方案,請:

function handleLoadSuccessful(evt:Event):void 
    { 
     evt.target.dataFormat = URLLoaderDataFormat.TEXT; 
     var corrected:String = evt.target.data; 
     corrected = corrected.slice(1,corrected.length-1); 
     var result:URLVariables = new URLVariables(corrected); 
     if (result.errorcode=="0") 
     { 
      for (var i:Number=0; i < result.n; i++) 
      { 
       liveOrderArray.push(
       { 
        code:result["ItemCode"+i], 
       qty:Number(result["LineQuantity"+i]) - Number(result["DespatchReceiptQuantity"+i]) 
       })      
      }  
      liveOrderArray.forEach(allocate); 
     } else { 
      trace("ERROR IN RUNNING QUERY"); 
      } 
    }   
    function allocate(element:*, index:int, arr:Array):void {     
       trace("code: " + element.code + " qty:" + element.qty); 
       allocationbible.profileCode = element.code.substring(0,1); 
       allocationbible.finishThk = Number(element.code.substring(1,3)); 
       allocationbible.longEdgeCode = element.code.substring(3,4); 
       allocationbible.backingDetailCode = element.code.substring(4,5); 
       allocationbible.coreboardCode = element.code.substring(5,6); 
       allocationBible = new allocationbible; 
       allocationBible.addEventListener("allocated", updateAllocationQty, false, 0, true); 
       trace("*************************************"); 
      } 
function updateAllocationQty (evt:Event):void {     
       //add result to array     
       trace(allocationbible.coreboardLongCode);    
      } 

回答

0

如果需要停止腳本的執行等待功能完成,然後指派事件是不是你想這樣做。你想要做的是你調用的函數返回你正在等待的值,根本不使用事件。

或許可以幫助更多的,如果我知道你在allocationbible

+0

element.code是一個30位商品代碼不同的子串等於不同的參數。根據這些參數,我計算出原材料的收益率,這是可分配計算的原材料。有沒有其他途徑比使用forEach?我指的是使用dispatchEvent獲取所需信息來計算分配的幾個sql表。 – user1344454 2012-04-19 17:13:32

0

在做什麼,你不能暫停for..each循環,這是不可能的AS3。所以你需要重寫你的代碼。

UPD:上次誤解了你的問題。要在進一步處理之前等待計算完成,可以開始處理分配事件處理程序中的下一個元素。是這樣的:

var currentItemIndex:int; 

function startProcessing():void { 
     // population of the array 
     // ... 
     allocationBible = new allocationbible(); 
     allocationBible.addEventListener("allocated", onAllocated); 

     currentItemIndex = 0; 
     allocate(); 
} 

function allocate():void { 
     var element:* = liveOrderArray[currentItemIndex]; 
     // configure allocationBible 
     allocationBible.process(element); 
} 

function onAllocated(e:Event):void { 
     trace("Allocated: " + allocationbible.coreboardLongCode); 

     // allocate the next item 
     currentItemIndex++; 
     if (currentItemIndex >= liveOrderArray.length) { 
      allocationBible.removeEventListener("allocated", onAllocated); 
     } else { 
      allocate(); 
     } 
}