Problem with for each loop and Url Loaders

Hello all :slight_smile:

I have a problem with loading profiles (users) into my stage. I want to first compare the profile I want to load in to the users allready on the stage, and if the user I want to load in have 2 or more interests in commen with the other, I want to load the profile in at the coordinats of the other and “paint” it the same color. The problem is that the first time I load a user in (when there are others loaded in) the variables lignendebruger and sammeinteresser is not set before the profilebox is loaded in to the stage. But when I remove and load the same user in again it works… Have tried to solve this problem for a very long time :slight_smile: and any help at all would be much apreciated. Hope you can get some kind of meaning out of the code, even though I have not uploaded all of it.

-Dardein

var urlLoaderInterest1:URLLoader = new URLLoader();
urlLoaderInterest1.addEventListener(Event.COMPLETE, gembrugerxml);
urlLoaderInterest1.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

var urlLoaderInterest2:URLLoader = new URLLoader();
urlLoaderInterest2.addEventListener(Event.COMPLETE, sammenligning);
urlLoaderInterest2.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

var bruger:XMLList;				

var lignendebruger:Number = 0;

var sammeinteresser:int = 0;

function nyProfilBox(n:Number){
// sets variables to som stardard
var farve:Number = Math.round( Math.random()*0xFFFFFF );
var CX:Number = randomBetween(0, 800);
var CY:Number = randomBetween(0, 600);

	//  this gets the interest of the user I want to load in
	var req1:URLRequest = new URLRequest("http://mywebserver.com?id"+n);
		req1.method = URLRequestMethod.GET;
		urlLoaderInterest1.load(req1);
	
	// this iterates throuh all the users curently on the stage and compares the interest of the user I want to load with the other users on the stage.
	for each(var user1:Number in bordListe1) {
		req2 = new URLRequest("http://mywebserver.com?id"+user1);
		req2.method = URLRequestMethod.GET;
		urlLoaderInterest2.load(req1);
	}
	// if the user has more than one interest in commen with another, I set the color of the new user to the one of the other
	if(sammeinteresser > 1){
		if(bordListe1.indexOf(lignendebruger) != -1){
			var	a:Profile_box_bordet = (Profile_box_bordet)(getChildByName("P"+(Number)(lignendebruger)));
			farve = ĂĄ.myColor;
			CX =a.container.x;
			CY =a.container.y;
		}
	}
	// make a new profile using the stardards or the same as one other profile curently on the stage
	var profile_mc:Profile_box_bordet = new Profile_box_bordet();
	profile_mc.myColor = farve;
	profile_mc.cX = CX;
	profile_mc.cY = CY;
	profile_mc.uID = n;
	profile_mc.name = "P"+n;
	addChild(profile_mc);
	bordListe1.push(n);
	lignendebruger = 0;
	sammeinteresser = 0;
}	

function sammenligning(e:Event) {
var gem:XML = (XML)(e.target.data);
var brugerallerede:XMLList = XMLList(gem.user.interests.children());
var antal:int = 0;
for each(var u:XML in bruger) {
if (brugerallerede.contains(u)) {
antal = antal +1;
}
}

	if(antal > sammeinteresser) {
		sammeinteresser = antal;
		lignendebruger = (Number)(gem.user.uid.text());
	}

}

function gembrugerxml(e:Event) {
var gem:XML = (XML)(e.target.data);
bruger = XMLList(gem.user.interests.children())
}

function randomBetween(a:Number, b:Number):Number {
return (a + Math.floor(Math.random()*(b-a+1)));
}