March 6th, 2005
Basic XML with Flash
Someone had asked me today how to use XML with Flash. He is interested in using a XML file to configure a game he wants to write. He wants to write a Monopoly-like game but be able to quickly reconfigure the game board based on the location he wants to use. I wrote up a very basic sample file for him and I thought I would share it here.
Actionscript:
-
var myXML:XML = new XML('<game><board name="item1" rent="250"/><board name="item2" rent="1000" /><board name="item3" rent="175" /><board name="item4" rent="550" /><board name="item5" rent="249.99" /></game>');
-
-
trace("Number of board nodes="+myXML.firstChild.childNodes.length);
-
numNodes = myXML.firstChild.childNodes.length;
-
for (i=0;i<numNodes;i++) {
-
trace(myXML.firstChild.childNodes[i].attributes.name +" has a rent of $"+myXML.firstChild.childNodes[i].attributes.rent);
-
}
Now this sample uses XML directly within the ActionScript, but it is done the same way using external XML, except you would have a myXML.load("config.xml") and then need a myXML.onLoad function handler.
