loveforvdubs
loveforvdubs

Reputation: 13

AS3 asynchronous troubles

I am trying to loop through an array and then fill another array as I do so. However the objects that I am putting into the new array take some time to calculate. This is giving me some scheduling headaches. I created a custom event to account for the completion of that calculation and tried to encorporate that into my loop, however, the function after the loop is still firing before even the first iteration of the loop is complete. Is there anyway to listen for the completion of the loop? Here is my code.

package  {

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import ImageSplitter;
import SplitEvent;
import Displayer;

public class DocumentClass extends MovieClip {

    private var _splitter:ImageSplitter;
    private var _imageParts:Array;
    private var _stageHeight:Number;
    private var _imgArray:Array;
    private var _bannerArray:Array = [];
    private var _displayer:Displayer;

    public function DocumentClass() {
        _stageHeight = stage.stageHeight;
        _imgArray = ["banners/banner1.jpg", "banners/banner2.jpg"];
        for (var i = 0; i < _imgArray.length; i++) {
            _splitter = new ImageSplitter(_imgArray[i]);
            _splitter.addEventListener(SplitEvent.SPLIT_COMPLETE, onSplit);
            function onSplit(e:SplitEvent):void {
                trace(e);
                _imageParts = _splitter.imageParts;
                _bannerArray.push(_imageParts);
                trace("Banner len: " + _bannerArray.length);
                _splitter.removeEventListener(SplitEvent.SPLIT_COMPLETE, onSplit);
            }
        }
        afterLoop();
    }



    private function afterLoop() {
        trace("loop finished. Banner len: " + _bannerArray.length);
    }
}   

Like I said above, afterLoop() fires before even one iteration of the for loop completes. Any help would be greatly appreciated.

Upvotes: 1

Views: 171

Answers (1)

Steve Lewis
Steve Lewis

Reputation: 1302

An easier method to do this would be to keep track of the total number of items in _imgArray in a new variable (in the same scope), and decrement it every time onSplit is fired. Once it reaches 0, your loop is finished and you can call afterLoop:

public function DocumentClass() {
    _stageHeight = stage.stageHeight;
    _imgArray = ["banners/banner1.jpg", "banners/banner2.jpg"];
    var numImages = _imgArray.length;
    for (var i = 0; i < _imgArray.length; i++) {
        _splitter = new ImageSplitter(_imgArray[i]);
        _splitter.addEventListener(SplitEvent.SPLIT_COMPLETE, onSplit);
        function onSplit(e:SplitEvent):void {
            trace(e);
            _imageParts = _splitter.imageParts;
            _bannerArray.push(_imageParts);
            trace("Banner len: " + _bannerArray.length);
            _splitter.removeEventListener(SplitEvent.SPLIT_COMPLETE, onSplit);
            numImages--; 
            if (numImages == 0) afterLoop();
        }
    }
    //afterLoop();
}



private function afterLoop() {
    trace("loop finished. Banner len: " + _bannerArray.length);
}

Upvotes: 1

Related Questions