Xml parsing doesn't work! (followed zachs tut)


(John Andersson) #1

Hey. Checked zach’s old tutorials to learn how to make tile maps etc, and not the new one since that one doesn’t cover tiles

I am using this code:

private function loadLevel(xml:Class):void { var rawData:ByteArray = new xml; var dataString:String = rawData.readUTFBytes(rawData.length); var xmlData:XML = new XML(dataString);

		var dataList:XMLList;
		var dataElement:XML;
		
		dataList = xmlData.Tiles.tile;
		
		for each(dataElement in dataList)
		{
			trace("HI");
			_tiles.setTile(int(dataElement.@x) / 120, int(dataElement.@y) / 120, int(dataElement.@tx) / 120);
		}
	}

But I get no trace!!

This is the tmx file:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="16" height="9" tilewidth="120"     tileheight="120">
 <tileset firstgid="1" name="u" tilewidth="120" tileheight="120">
  <image source="../assets/tileset1.png" width="360" height="120"/>
 </tileset>
 <layer name="Tiles" width="16" height="9">
  <data>
   <tile gid="2"/>
   <tile gid="2"/>
   blablabla repeat
  </data>
 </layer>
 <objectgroup name="Spawn" width="16" height="9">
  <object x="120" y="892" width="122" height="28"/>
  <object x="2222" y="-244"/>
 </objectgroup>
</map>

I followed this tutorial: https://www.youtube.com/watch?v=Ek0HLk6wBmQ Why am I not receiving a trace statement? It just doesn’t work


(Ultima2876) #2

How are you embedding and passing your XML to the function? The lack of trace indicates that the dataList is empty, meaning it’s probably an issue with how the XML is being embedded or how it is being passed to the loadLevel function.


(Jacob Albano) #3

You create your XmlList like this:

dataList = xmlData.Tiles.tile;

But there’s no element called “Tiles” in your document.

Try this:

dataList = xmlData.layer.(@name == "Tiles").data.tile;

(Ultima2876) #4

Ah, I should’ve noticed that. Good spot @jacobalbano


(Zachary Lewis) #5

It looks like you’re using the XML format generated by Tiled. I believe my tutorial was based on the XML format generated by Ogmo Editor.


(John Andersson) #6

I changed it, and now I get 1 block on the screen. I don’t get a trace statement however… which is odd, because if I remove

		for each(dataElement in dataList)
		{
			trace("HI");
			_tiles.setTile(int(dataElement.@x) / 120, int(dataElement.@y) / 120, int(dataElement.@tx) / 120);
		}

then I get no block on the screen either…

Hmm, wonder why I get one block only. The XML file contains a lot of

   < tile gid="2"/> 

lines and

   < tile gid="1"/>

lines.

The tiles with the number 1 are the grond, and I want the

		for each(dataElement in dataList)
		{
			trace("HI");
			_tiles.setTile(int(dataElement.@x) / 120, int(dataElement.@y) / 120, int(dataElement.@tx) / 120);
		}

to generate the ground tiles. What is missing? :stuck_out_tongue: Thank you all so much for helping me. I love you guys. This is the most friendly and well done forum I’ve ever seen.

EDIT: I tried changing it to this

dataList = xmlData.layer.(@name == "Tiles").data.tile.(@tile gid == "1")

But (as I suspected), the space between “tile” and “gid” messes things up. And I’m not so sure it would work if it was called “tilegid” in the first place either :stuck_out_tongue:

EDIT 2: I realized that it already says “data.tile”, so it already references the tiles. So I tried changing it to this:

dataList = xmlData.layer.(@name == "Tiles").data.(@tile == "1");

and… well… this

dataList = xmlData.layer.(@name == "Tiles").data.(@gid == "1");

But it didn’t work :stuck_out_tongue: Trying to figure it out for myself, because I’d like to LEARN. But wow, this stuff is confusing. Trying to come up with the solution on my own in this case (XML, it’s new to me) is like trying to “realize” how math works without anyone telling you! :blush:

EDIT 3:

Progress! Looked at the previous code, and changed it to this:

dataList = xmlData.layer.(@name == "Tiles").data.tile.(@gid == "1");

at least I get no errors now, but only one block is still displayed! Hmm. I*m starting to think that the XML parser doesn’t translate the block positions from the XML file, as it doesn’t contain any X and Y values. It’s like 2D arrays, you don’t really see the positions in numbers, it’s just laid out in a way that is visually understandable. Question might be how to change the XML parser to translate the XML file’s block positions! Or maybe it’s this line:

_tiles.setTile(int(dataElement.@x) / 120, int(dataElement.@y) / 120, int(dataElement.@tx) / 120); 

Since it is like the tutorial, and as you said Zach, it follows the ogmo way. I guess I gotta change that line to something else… This is a tough one.

I tried checking the Tiled documentation @ http://gamedevelopment.tutsplus.com/tutorials/parsing-tiled-tmx-format-maps-in-your-own-game-engine--gamedev-3104 , but that is a TON of code! Is all that really necessary? I tried adding it into my project but it was so confusing. I don’t really know where to put everything.

I remember trying Ogmo, but the XML file isn’t saved the same way as in your tutorial. I’m so lost :frowning:


(John Andersson) #7

Okay, so I followed http://www.youtube.com/watch?v=TPPzgB3fv8A and made it work. I dislike OGMO, however. Tiled is so much nicer. So I’d still appreciate an answer if anyone knows it ^^ Thank you so much


(Ultima2876) #8

You are correct; Tiled doesn’t seem to output any tile position data, instead just a list of tiles and their ‘gid’. In this case, you have to use the ‘layer’ width and height to work out where the tiles should be placed, and use a for loop that calculates the x and y positions of the tiles for you. You seem to have learned how to read specific values from the XML, so I’ll leave that as an exercise for you, but your code will look something like this:

var tileWidth: int = TILE_WIDTH_FROM_XML; //should be 120 according to your XML
var tileHeight: int = TILE_HEIGHT_FROM_XML;
var layerWidth: int = LAYER_WIDTH_FROM_XML; //should be 16 from XML
var layerHeight: int = LAYER_HEIGHT_FROM_XML; //should be 9 from XML
var numTiles: int = layerWidth * layerHeight; //work out how many tiles
var xx: int = 0;
var yy: int = 0;
for(int i = 0; i < numTiles; i++)
{
  _tiles.setTile(xx, yy, int(TILE_GID_FROM_XML)); //set the current tile
  xx++; //increment x position
  if(xx >= layerWidth) //end of row, increment y and reset x tile position
  {
    xx -= layerWidth;
    yy++;
  }
}

(Jacob Albano) #9

Alternately, you may try using the TMX parser that already exists for Flashpunk.


(John Andersson) #10

Thank you all! You can close this thread :smiley: Now everyone go help me out in my other threads mohahaha >:]


(Ultima2876) #11

This topic was automatically closed after 2 days. New replies are no longer allowed.