First off: Thank you, Zach Lewis for all the tutorials and examples. I’m loading data from Tiled’s TMX(XML) format. I created a class that I can call from my GameWorld. I adapted your code here, and it works like a charm!
However, if I add another layer in Tiled I get an “invalid bitmap data” error when I run the program. I’ve confirmed that it’s properly reading the layer I have. Everything works perfectly as long as there’s only one map layer in the Tiled data. I can get it to only add tiles from a only a given layer name, but if there’s a second layer at all I get the Invalid Bitmap Data error.
Any clues what might be going on? Here’s the relevant code:
package levels
{
import flash.utils.ByteArray;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Tilemap;
import net.flashpunk.masks.Grid;
import worlds.GameWorld;
import net.flashpunk.FP;
/**
* ...
* @author Danceatron
*/
public class TestLevel extends Entity
{
public function TestLevel(xml:Class,offset:int) //xml==chunk offset == x pixels wide of chunk
{
var _map:Tilemap;
var _grid:Grid;
var mapXML:XML = FP.getXML(xml);
var mapWidth:uint = uint(mapXML.layer.@width);
var mapHeight:uint = uint(mapXML.layer.@height);
var tileX:uint = 0;
var tileY:uint = 0;
// Create a tilemap to show the level.
// Tile size is hardcoded, but could be pulled from the XML.
_map = new Tilemap(GC.LEVEL1_SPRITESHEET, mapWidth * 50, mapHeight * 50, 50, 50);
_grid = new Grid(mapWidth* 50, mapHeight * 50, 50, 50, 0, 0);
mask = _grid;
type = GC.TYPE_PERMWALL;
for each (var layer:XML in mapXML.layer)
{
trace(layer.@name);
if (layer.@name == "map")
{
// Iterate through tiles, adding them to the tilemap.
for each (var tile:XML in mapXML.layer.data.tile)
{
// Once the end of the map is reached, loop back to the start.
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
// Ignore empty tiles.
if (tile.@gid != 0 )// && layer name="map"
{
_map.setTile(tileX, tileY, uint(tile.@gid - 1));
_grid.setRect(tileX, tileY, 1, 1, true);
}
// Move to the next tile.
tileX++;
}
// Add the map to the world.
addGraphic(_map);
tileX = 0;
tileY = 0;
}
else
{
//trace("DO NOTHING");
}
}
}
}
}
Thank you fine people for your help.