PDA

View Full Version : XML information loop?



AstralAron
February 14th, 2008, 07:51 AM
I just did a tutorial here on kirupa about displaying XML data in flash, does anyone know of a tutorial on how to loop the information? so it displays all the different 'nodes' i guess u call them?

bigbstanley
February 14th, 2008, 03:29 PM
I just did a tutorial here on kirupa about displaying XML data in flash, does anyone know of a tutorial on how to loop the information? so it displays all the different 'nodes' i guess u call them?


When you are reading the XML file in flash, you usually store the information in an Array. So you would want to just play the information then in the array, playing one index of it at a time.

So for example: (you would need an XML file first to run this)

// variable for initial node in XML
var xmlNode = this.firstChild;
// variable to hold the total nodes in the XML
var total:Number = xmlNode.childNodes.length;
// create your Array object
var myArray:Array = new Array();

// loop through XML and store data into Array Object
for(i=0; i<total; i++) {
myArray[i] = xmlNode.childNodes[i].attributes.whateverYouWant;
}

// so to see your information you just call the correct index
// remember though we started the loop with 0, so everything is offset by one number
// like if you wanted to see the second value in your array
trace(myArray[1]);



Here is another example you can run without an XML file:


// index variable
var p:Number = 0;

// your Array object with variables
var myArray:Array = new Array();
myArray = ["value1", "value2", "value3", "value4", "value5"];

var total:Number = myArray.length;

// function to run another function every few seconds (the name of the function, time to run it in milliseconds
function myInterval(){
nextIndex = setInterval(increment, 1000);
}

// incremental function
function increment(){
//clears the Interval timer, otherwise this will exponentially get faster
clearInterval(nextIndex);
//trace out the value
trace(myArray[p]);
//increase your index
p++;
// basically this is an if statement saying that if p is less then the total, keep the value of p, otherwise the index goes back to 0
p = (p<total) ? p : 0;
// run the interval function
myInterval();
}

// run the initial function
myInterval();