Each tileset has a
firstgid property in the TMX, e.g.:
<tileset firstgid="1" source="tilesetA.tsx"/>
<tileset firstgid="512" source="tilesetB.tsx"/>
The tileset with the smallest
firstgid equal to or greater than the tile
gid you’re looking for is the tileset you want. In this example, if you have a tile with
gid 300, it’ll be part of tileset A because 300 is greater than 1 but less than 512, the next smallest
firstgid. A tile with
gid 600 will be part of tileset B, because 600 is greater than 512. You will need to do this for each tile.
When parsing, remember to first read and strip the flipping flags before you do these comparisons, as the flags can make the
gid look like a much larger number.