views:

369

answers:

1

I had posted my question as an answer to this question: How to get associated URLRequest from Event.COMPLETE fired by URLLoader

Sorry. Here is my question again:


Hi, How can I make your function work for loader object in a loop? Thanks! Meengla Here is my existing (rough) code; I always get the mylabel from the last element of the array.

var _loader = new Loader(); 
for (j = 0; j < 5; j++) {
    //mylabel variable is correct setup in the loop
    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
        doneLoad(e, mylabel);
    });

    _loader.load(new URLRequest(encodeURI(recAC[j].url)));

}//for loop
A: 

As per the comments above, this won't work because:

1) You're just adding the same event listener 5 times to the loader. 2) You're just reseting your same loader object 5 times.

The final output will just be as though you only called it the last time.

There are a variety of ways to address this - loading stuff asynchronously is one of the great mindfucks of learning to code - but the simplest way is probably just to create five separate loaders.

I'd do something like this:

var loaders:Array = [];
var labels:Array = ["label1", "label2", "label3", "label4", "label5"];
for (var j:int = 0; j < 5; j++) {
loaders[j] = new Loader();
loaders[j].contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loaders[j].load(new URLRequest(encodeURI(recAC[j].url)));
}

function completeHandler(e:Event):void {
doneLoad(e.currentTarget, labels[loaders.indexOf(e.currentTarget)]);
}

The confusing part is finding a good way to keep track of which load is associated with which label etc, since in theory your loads can finish in any order. That's why I've got a separate label array there, and then you just match up the desired label with the loader that just finished loading.

I hope that helps!

Myk
Hi, I think I am close! But this expression is not being resolved, according to Flex debugger:labels[loaders.indexOf(e.currentTarget)]);
meengla
Hmm, it should be... Arrays have the indexOf() method which returns the index of the item you're passing in, if it is in the array. Not sure what would cause that not to work? There are other ways to address this - you could use a dictionary instance, for instance. in your loop, dict[loaders[j]] = labels[j] then in your handler just something like: doneLoad(e.currentTarget, dict[e.currentTarget])A million ways to solve it, but it sounds like you're close. Just play with it. :)
Myk
Yes, the 'loaders' array does have a bunch of 'loader' objects but the matching should be exact in case indexOf()
meengla
I think I have fixed the problem using your solution but with slight modification! The e.currentTarget was returning the 'url' of the image whose bytes were being returned from the server.doneLoad(e, labels[urls.indexOf(e.currentTarget.url)]);simply creating a new 'urls' array and populating it with urls within the loop did the job. I told ya I was close!Thanks for your help!!
meengla