Saturday, September 06, 2008

[XML Parsing] 04: Reading Attributes

04: Reading attributes

 

I started making a test example just using the one row, just to see if it would work, and it wouldn't, and I got confused again, and looked over my notes, and on other pages, and wasted a lot more time :)

But I think I'm back on track again now (hopefully) and attributes is the key. It's not like the other stuff wasn't relevent, it was, but I will be expanding on it by looking at how attributes can be used to get data.

Firstly, I would just be repeating what it says HERE if I were to explain the use of attributes in XML myself.

 

Because I am now using attributes, the old xml file is no use.  I have edited my code taking the new file's location into concideration, so you don't have to.

 

var xmlLoader:URLLoader = new URLLoader();

 

var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("http://83.170.89.79/~gibboco/
blog/tutorials/xml/test2XML.xml"));

 

function LoadXML(e:Event):void
{


xmlData = new XML(e.target.data);
parseThis(xmlData);


}

 

function parseThis(myInput:XML):void
{

var blockAttributes:XMLList = myInput.row.block.attributes();

for each (var blockValue:XML in blockAttributes) {
   
trace(blockValue);

}


}

 

This example simply displays the attributes of the new file in the output. 

In the code highlighted orange above, myInput is the whole XML data.  We are then going through the children and the children's children (row and block) getting the grandchildren's attributes.  The variable blockAttributes grabs the value of all of this.

 

In the pink highlighted code, we are looping through each instance of a new copy of XML, 'blockValue' within blockAttributes.  For each instance of blockValue in blockAttributes, it traces back the value of blockValue (outputting it on the screen).

 

 

 

 

[XML] Loading level layouts into tile based Flash Game

Introduction


I am starting the XML stuff for my wizards game from scratch. Yesterday after trying several methods of getting multiple levels to load, I found out about using XML to load in data to a flash file.  I used several websites to help me understand the concept (Emanuel Feronato,8 Bit Rocket and by far the most useful and easy to understand, Kirupa).

 

Anyway, thinking I had learnt completely how to use it, I dived straight in to adding the code into my project.  This sort of worked.  I am still unsure why it DIDN'T work completely.  I managed to load in the level and display it on the screen, but I couldn't work out why it wasn't showing properly (the tiles were all the same, as oposed to showing the correct tile based on the number in the XML file). 

 

So, this is my blog of doing all the XML walkthrough stuff in a blank project, without any of the other code or anything else confusing me.  It's also sort of a tutorial, so follow it if you like.  It may not be the best way of doing the thing I am trying to acheive, but I will try and explain what I am doing as I go along. 

 

 

[XML Parsing]03: Creating the level tiles

03: Creating the level tiles

 

For my game, I want a way of displaying tiles that look different depending on the number which appears in between each <tilecol> tag.

 

To achieve this I will need a for loop, which will will loop through every row, and within that loop, have a second loop which will loop through every column.

 

Within the second loop there shall be some code which creates each individual tile, putting it into the correct location based on its row and column position and change its frame number to the number grabbed from the xml information.

 

The below images show what I am aiming to do: (note: upload images)

 

(needs finishing)

[XML Parsing] Accessing Data

2: Getting the information we need

 

Replace all the code from before with this

 

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("http://83.170.89.79/~gibboco/
blog/tutorials/xml/myXML.xml"));

function LoadXML(e:Event):void
{
       xmlData = new XML(e.target.data);
       parseThis(xmlData);
}

. function parseThis(myInput:XML):void
{
      trace(myInput.tilerow[6].tilecol[2]);
}

 

This time, instead of tracing the xmlData in the LoadXML function, we call a function 'parseThis' which we send the xmlData to.

 

The above example displays the second 'tilecol' items within the sixth 'tilerow' item (shown in the pink highlighted areas).

[XML Parsing] The Loader

1: Loading the XML File

 

I am not going to go into loads of detail about what everything does, for now anyway, just the code and what some of the bits do.  I will however highlight the bits you won't need to change and the bits you will.

 

To load the xml file:

 

(Actionscript 3.0 code)

 

01. var xmlLoader:URLLoader = new URLLoader();
02. var xmlData:XML = new XML();

03.

04. xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

05. xmlLoader.load(new    

     URLRequest("http://83.170.89.79/~gibboco/blog/
     tutorials/xml/myXML.xml"));

06. function LoadXML(e:Event):void
07. {
08.     xmlData = newXML(e.target.data);
09.     trace(xmlData);
10. }

 

01 to 04: Can be left alone, just sets stuff up for you (if you change 'LoadXML' on line 4, you will have to change the function name in 06 as well).

 

05: Loads in your xml file.  This bit you need to change to the location of your xml file or use the one I have uploaded (used in the example).  The kirupa example uses the url http://www.kirupa.com/net/files/sampleXML.xml which displays some info on books, which the kirupa tutorial uses for showing information.

 

06 to 10: The function.  Can be left alone.  This sets 'xmlData' to the value of the content of your XML file and displays it in the output (by tracing xmlData).

 

My XML file: myXML.txt (you'll need to save it as an xml file) (if you want to have it link to an internal file on your computer)

 

 

 

 



Wizards 2.0 Game

I think I’ve gone mad.  I can’t for the life of me work out what I have done wrong here.  The number between each <titlecol> tag gives the tile numer which should be applied to the tile when it loads.  There are three tiles, tile0 (blank), tile 1 (the 2x2 blocks you can see in the image with grass on the top) and a third tile, tile 2 (a 2x2 block of the same tile without brown on). The ordering of the <tilecol> tags determines where the tiles in that row go, and the different <tilerow> determines on what row it’s column’s tiles go.

 

 

(above: the level design)

<map>

<tilerow>

 

<tilecol>1</tilecol>
<tilecol>2</tilecol>
<tilecol>1</tilecol>
<tilecol>2</tilecol>
<tilecol>1</tilecol>
<tilecol>2</tilecol>
<tilecol>2</tilecol>
<tilecol>2</tilecol>
<tilecol>2</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>
<tilecol>0</tilecol>


</tilerow>

 

the xml code (sample):

 

 

 

 

 

(above: the three different tiles)

 

If it was that somehow the tile 1 tiles were obscuring the tile 2 tiles, being sized wrong or something then I could  understand this, but I tested it by making the tiles on the top row ‘1,2,1,2,1,2,2,2,2’ and when I run the game it is plainly obvious that there is nothing there, it’s not putting a block down!

 

I don't know why I think writing a blog about it will help, but I may realize something stupid I have missed.

 

Well I havn't yet.  Anyway I have lots more stuff to add to the blog, loads more screen shots and things, which I will get around to doing eventually.

 

(note: I am using Adobe Contribute CS3 to edit this blog.  I previously tried using Microsoft Word which DOES have the ability to edit blogs, but I couldn't get it to upload images, and I found that other people had this problem by googling my problem.  However, I am having the same problem with Adobe Contribute although I can't see anything on the internet about similar problems).