PDA

View Full Version : XML or function problem?



estroina
February 17th, 2009, 06:35 AM
I'm having a problem here and I can't explain it.
I have an fla that opens an XML and retrives the values, here's the code:


import flash.xml.*;

var ids:Array = new Array();
var titulos:Array = new Array();
var descricoes:Array = new Array();
var imagens:Array = new Array();
var anos:Array = new Array();
var n:int;

var objects_array:Array=new Array();
var colonne_array:Array=new Array();

var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeHan dler);

var request:URLRequest=new URLRequest('projectos.xml');
try {
loader.load(request);
} catch (error:Error) {
trace('Impossivel carregar o documento!');
}

function completeHandler(event:Event):void {
var loader:URLLoader=URLLoader(event.target);
var result:XML=new XML(loader.data);

var myXML:XMLDocument=new XMLDocument();

myXML.ignoreWhite=true;
myXML.parseXML(result.toXMLString());
var node:XMLNode=myXML.firstChild;

n=node.childNodes.length;

for (var i:int=0; i < n; i++) {
ids[i]=node.childNodes[i].childNodes[0].firstChild.nodeValue;
titulos[i]=node.childNodes[i].childNodes[1].firstChild.nodeValue;
descricoes[i]=node.childNodes[i].childNodes[2].firstChild.nodeValue;
imagens[i]=node.childNodes[i].childNodes[3].firstChild.nodeValue;
anos[i]=node.childNodes[i].childNodes[4].firstChild.nodeValue;
}
}

But if in the next frame I try to use the values on vars, n, ids, titulos, etc all are empty... Can anyone explain me why? When I define the value inside a function that isn't updated outside?

wvxvw
February 17th, 2009, 06:38 AM
1. Why would you use legacy XMLDocument instead of XML?
2. You probably navigate to the next frame before the XML loads.

estroina
February 17th, 2009, 07:05 AM
1. Why would you use legacy XMLDocument instead of XML?
2. You probably navigate to the next frame before the XML loads.

1.Is there any tutorial with the new way?
2. Maybe you're right, is possible to use a preloader for that?

wvxvw
February 17th, 2009, 09:05 AM
Where did you find the tutorial for XMLDocument? I haven't seen even one... All the tuts for AS3 I've seen are about XML, not XMLDocument.
Ugh... if you already have frames, then I'd simply call stop() in the first frame and play() inside the completeHandler.