Problem with for loops

Hi there,

I’m having some issues with this loop:

[AS]for(var i=0; i<10; i++){
_root[‘myXML’ + i] = new XML();
_root[‘myXML’ + i].ignoreWhite = true;
_root[‘myXML’ + i].onLoad = function(ok) {
if (ok) {
allContent* = this.firstChild.firstChild.nodeValue;
amLoaded* = true;
}
}
_root[‘myXML’ + i].load(‘content/’ + names* + ‘/text.xml’ );
}[/AS]

So, I know there is an issue with this kind of thing. I use a similar script to allocate actions to MCs, but with an MC, I can store a variable inside it, so it knows who it is. With this script above, because they all come in at the same time, it only shows one lot of content.

Any suggestions on how to rewrite it? Please bare in mind, that I am actually trying to load them all at once, and not sequentially (if that makes sense)

Thanks.

maybe make sense to load one xml in, then start the loop with the next :stuck_out_tongue:

there not loading in at once if you have a for loop, they are still techniquely running a millisecond behind each other

I think helps to know what app it is for

Create a recursive function:


var howManyXMLs:Number = 10;
var count:Number = 0;

function loadXML(xml) {
	xml = new XML();
	xml.ignoreWhite = true;
	xml.load('content/'+names[count]+'/text.xml');
	xml.onLoad = function(ok) {
		if (ok) {
			allContent[count] = this.firstChild.firstChild.nodeValue;
			amLoaded[count] = true;
			count++;
			if(count<10){
				loadXML(_root['myXML'+count]);
			}
		}
	};
}

loadXML(_root['myXML'+count]);

Thanks RubenFlash, that worked a treat! Can you tell me why that works and a for loop doesn’t? For me that seems to be doing the same thing?