Parsing TMX (XML) from Tiled: Invalid bitmap data error when more than one layer


(Robert) #1

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.


Invalid bitmapdata.. xml and even MORE annoying stuff to deal with
(Jacob Albano) #2

One possibility is that you’re overwriting your _map variable each time you run through the loop. I don’t see why that would cause that particular error though.

What line is the error originating from?


(Robert) #3

It’s definitely something with the _map variable. The fault occurs right after declaring the tileX and tileY variables. I presume it’s while creating_map as a new TileMap.

So it’s happening right here:

            _map = new Tilemap(GC.LEVEL1_SPRITESHEET, mapWidth * 50, mapHeight * 50, 50, 50);

(Jacob Albano) #4

Okay, I think I see the problem.

You’re doing this: mapWidth * 50, mapHeight * 50. I feel like that’s going to result in an image that’s way larger than the maximum size a BitmapData can be. The thing that gives me pause is the fact that you claim it works until you add another layer. Is your first layer much smaller than your second?


(Robert) #5

See, that’s the thing… The layers are the same size, I even made them smaller just to test. Even if the additional layer is empty I still get the error.

UPDATE: Decided to try this in Zach’s Example Here. I can re-create the error by simply compiling Zack’s project, then going into Tiled and adding a second Tile Layer (even if it’s empty) to the sample.tmx.

[Fault] exception, information=ArgumentError: Error #2015: Invalid BitmapData.

(azrafe7) #6

Try initializing tileX and tileY to 0 inside the layer loop and see if it works.


(Robert) #7

No dice on that one. Same error.


(azrafe7) #8

Found out what’s wrong with it. You have to move all the initializations inside the layer loop.

You can see what’s going on by tracing the value of mapWidth and mapHeight.


(Jacob Albano) #9

Wow, good catch. XMLLists are weird.