Hello,
I’ve got a small XML problem. I looked at the tutorial on the site for handling XML with ActionScript 3. My goal is to make an external file which will hold ‘maps’ for my game. I currently got it stored like this:
<Maps>
<Map LEVEL="1">
<rij>0101010101010101</rij>
<rij>0100000000000401</rij>
<rij>0101010001010101</rij>
<rij>0100000001000001</rij>
<rij>0100010101010001</rij>
<rij>0100010000000001</rij>
<rij>0103000001010201</rij>
<rij>0101010101010101</rij>
</Map>
<Map LEVEL="2">
<rij>010101010101</rij>
<rij>010400000001</rij>
<rij>010101010001</rij>
<rij>010000000001</rij>
<rij>010001000101</rij>
<rij>010301000001</rij>
<rij>010101010001</rij>
<rij>010200010001</rij>
<rij>010100000001</rij>
<rij>010101010101</rij>
</Map>
</Maps>
Now my question is the following. I want to read these data from into an array map. So far so good. Only I want to be able to seperate all the values. So i would have to take the first 2 characters of a string, and add it into an array inside the map array, so I could access it like this: map[0][0]. I think the problem is that the data is XML, not String. How can I make this happen?
Here is my ‘read the file and make an array’-code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Henk extends MovieClip
{
private var xmlLoader:URLLoader;
private var xmlData:XML;
private var currLevel:int;
private var map:Array;
public function Henk()
{
currLevel = 2;
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("Levels2.xml"));
}
private function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
parseMap(xmlData);
}
private function parseMap(mapInput:XML):void
{
var rijLijst:XMLList = mapInput.Map.(@LEVEL == currLevel).rij;
map = new Array();
for(var i:Number = 0; i < rijLijst.length(); i++)
{
var rijElement:XML = rijLijst*;
var rijString:String = rijElement.toString();
for(var j:Number = 0; j < (rijString.length / 2); j++)
{
map.push(rijString.substr(j*2,2));
}
}
for(i = 0; i < map.length; i++)
{
trace(map*);
}
}
}
}
EDIT: thought i’d update the parseMap function here aswell.