Can't use variable from outside XML onload function

I am having trouble passing an array through a function, and then accessing the array from outside the function, here is my code:


           speechArray = new Array();
 function loadXML(loaded) {
      var arrayVar:Number = 0;
      if (loaded) {
           mainArray = new Array();
           for (i=0; i<this.firstChild.childNodes[2].childNodes.length; i++) {
                mainArray* = new Array();
                for (j=0; j<this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes.length; j++) {
                     mainArray*[j] = new Array();
 for (k=0; k<this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes[j].childNodes.length; k++) {
mainArray*[j][k] = new Speech(this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes[j].childNodes[k].attributes.speaker, this.firstChild.childNodes[2].childNodes*.childNodes[0].childNodes[j].childNodes[k].firstChild.nodeValue, i+1, j+1, {ra:100, rb:200, ga:100, gb:20, ba:100, bb:123, aa:100, ab:0}, 0, -100, 100, 200, 200);
                          speechArray.splice(arrayVar, 0, mainArray*[j][k]);
                          arrayVar++;
                     }
                }
           }
      }
      trace(speechArray[0].getspeaker());
 }
 xmlData = new XML();
 xmlData.ignoreWhite = true;
 xmlData.onLoad = loadXML;
 xmlData.load("roj-play.xml");
 

I can trace the Array from inside the loadXML function, but not outside it, which is what I want to do. I have read Senocular’s XML specificIssues3 page, and tried the suggestions, but I am not sure which is the best one to use, and how exactly they apply to my specific problem. Any suggestions would be greatly appreciated.

Thanks!

Oh, I tried declaring the variable on the _root, _global, _parent…did not work. Undefined all the time.

you’re tracing the variable before its defined. The onLoad event occurs after all the code in that script has been executed. Its purpose for existing is because it takes time to load external information. You cant access a variable defined in an onLoad prior to when the onLoad is fired.

Thanks for the explanation.

I found a way around this though.

On my first frame of my main timeline:


_quality="BEST";
xmlCanais = new XML();
xmlCanais.ignoreWhite = true;
xmlCanais.load("conteudo.xml");
stop();
this.onEnterFrame = function(){
	if (_root._framesloaded == _root._totalframes && xmlCanais.loaded == true){
		delete onEnterFrame;
		play();		
	}
}

So it only starts to play when everything’s loaded.

Later on this is what I got inside my movieclip:


var dragBottom:Number;

function carregaXML () {
	if (_root.xmlCanais.status == 0) {
		main.descricao.text = _root.xmlCanais.firstChild.childNodes[2].firstChild.childNodes[1] add _root.xmlCanais.firstChild.childNodes[2].firstChild.childNodes[2];
		
		main.mcCanaisLinks._y = 		
		dragBottom = main.descricao.textHeight + 300;		
		
		aLinks = _root.xmlCanais.firstChild.lastChild.firstChild.childNodes;
		nLinks = aLinks.length;
		
		main.mcCanaisLinks.mcLinks.mcItem._visible = false;
		criaBotoes();
		stop();
		} else {
			gotoAndPlay(1);
			};
	};

this.onEnterFrame = carregaXML();

main.setMask(mask_mc);
fscommand("allowscale", "false");
bar.useHandCursor = dragger.useHandCursor=false;
space = 30;
friction = 0.9;
speed = 4;
y = dragger._y;
top = main._y;
bottom = 0 - dragBottom;
trace(dragBottom);

The **dragBottom **variable is what I wanted to access. That last trace now returns me some negative value, exactly what I wanted. Works perfectly.

I could have used the loaded property to check if my XML is loaded ok, or even better, used no if at all since I already know my XML is loaded.

:azn: