XML is a very good and importent way to work with external data in flash, it's easy, implented and efficient to save levels, maps, and data in general. I will cover the basics of loading XML and processing it here.
First we learn XML
There really isn't much to learn about xml, there are no commands or anything, it's a way of storing data. we start with the type we're saving for example level, then we put the attributes followed by the data. for example here is what I used. remmember, every tag we open we close.
<level id='1' name="Forest of Home>
</level>
there , I created a new level, now let's pour some data in, there are 2 ways of storing data in xml, like this:
<data id=1 />
no need to close the tag, we gave it an attribute and it's one
and
<data>1</data>
this is the other way, I ususally preffer the first way, this tutorial isn't about teaching xml, so if you're interested in actual xml this is an excellent tutorial
opening an xml file
The first thing we need to do is open the xml file we're parsing, for this tutorial i'm using the following file
<data>
<Message text="Welcome to Godlimation" />
<Message text="Site Created By Inglor" />
<Message text="This site belongs to Patrick, creator of Xunmato" />
<Message text="Visit our new link section" />
<Message text="The user system, is N/A yet" />
<Message text="Mo likes to touch little kids" />
<Message text="David and Goliath, coming soon" />
</data>
this is an XML document I've created somewhat not too long ago for a site I'm working on, it's named "data.xml".
now let's open it in flash, the first thing we need to do is create a new XML handler object
this is done the following:
Messages=new XML();
I named it Messages, I could have picked any other name.
the second thing we must do is clear out all the white spaces, this is very importent since that way our data is clean and " "s in our XML are ignored.
this is done the following:
Messages.ignoreWhite = true;
the next thing we need to do is load the actual xml file into flash, this is done with the load command,
Messages.load('data.xml');
now we have an XML object handler with all the XML data inside it, all we have to do now is process it.
Parsing the XML
now messages has several "childs", these are the contents of him
Messages.onLoad = function(success) {
if (success) {
readMessages();
}
};
every time you open a XML file always include a onLoad function, this is what triggers when the XML loads
my read messages function is the following
Messages.onLoad = function(success) {
if (success) {
readMessages();
}
};
function readMessages(){
_root.a=new Array();
for(i=0;Messages.firstChild.childNodes[i]!=undefined;i++){
base=Messages.firstChild.childNodes[i].attributes.text;
trace(base);
_root.a.push(base);
}
_root.delay=0;
_root.onEnterFrame=function():Void{
if(_root.delay==0){
this.message=this.a[random(this.a.length)];
_root.delay=60;
}
_root.delay-=_root.delay>0;
}
}
I will explain the terms now
for(i=0;Messages.firstChild.childNodes[i]!=undefined;i++){
this runs untill the node of the first child (which are the <message> tags since they are nodes of the <data> tag) is undefined meaning, untill thre are no more messages to read.
base=Messages.firstChild.childNodes[i].attributes.text;
the firstChild is data, the child node is the current message, now attributes are the data, and text is the attribute I'm addressing (since it's text="blablabla" in the XML file)
trace(base);
_root.a.push(base);
and I trace it and push it into a data array, now after the loop ends I have all my data in the array and I'm free to process it ;)
Hope this tutorial contributed, I know it's kinda confusing, please ask any questions.