Version
- Operating system: MacOS 10.13.6
Description
I'm building a 2d platformer, making heavy use of TILED's Object Layers. I found that when I delete an object from a Tileset (Collection of Images), TILED does not re-use the internal id of the deleted object. So when developing a game, one usually add objects to a tileset, removes them, moves them to another Tileset that makes better sense to have it in, etc. That might left us with Tilesets with internal id's that are not consecutive, for example:
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.1" name="Castle" tilewidth="300" tileheight="913" tilecount="7" columns="1">
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="2">
<image width="300" height="350" source="../assets/castle/InnerGateBottomBG.png"/>
</tile>
<tile id="3">
<image width="300" height="450" source="../assets/castle/InnerGateTop.png"/>
</tile>
<tile id="4">
<image width="65" height="270" source="../assets/castle/Portcullis.png"/>
</tile>
<tile id="5">
<image width="300" height="500" source="../assets/castle/TowerBottom.png"/>
</tile>
<tile id="6">
<image width="300" height="450" source="../assets/castle/TowerTop.png"/>
</tile>
<tile id="10">
<image width="300" height="500" source="../assets/castle/InnerGateBottomBase.png"/>
</tile>
<tile id="12">
<image width="163" height="913" source="../assets/near/NearColumn.png"/>
</tile>
</tileset>
In that .tsx file (TILED external tilesets files) we see that the tile id's 0,1, 7, 8, 9 and 11 are missing, they were deleted by me in the process of building the levels. The tile id's are not consecutive: (2, 3, 4, 5, 6, 10, 12) instead of (0, 1, 2, 3, 4, 5, 6).
This is ok within TILED, cause the internal gid is calculated as firstgid + internal id of a tile in a tileset.
I saw that when I deleted some object from a tileset in TILED, Phaser would show me objects (declared within that Tileset/ImageCollection) with different images than they were supposed to have.
I tracked the problem to be within Phaser.Tilemaps.Parsers.Tiled.ParseTilesets
Currently it has :
var newCollection = new ImageCollection(set.name, set.firstgid, set.tilewidth,
set.tileheight, set.margin, set.spacing, set.properties);
for (stringID in set.tiles)
{
var image = set.tiles[stringID].image;
var gid = set.firstgid + parseInt(stringID, 10);
newCollection.addImage(gid, image);
}
imageCollections.push(newCollection);
stringID is the index within the array of tiles, NOT the id of the tile.
It should be something like this:
var newCollection = new ImageCollection(set.name, set.firstgid, set.tilewidth,
set.tileheight, set.margin, set.spacing, set.properties);
for (t = 0; t < set.tiles.length; t++)
{
var tile = set.tiles[t];
var image = tile.image;
var gid = set.firstgid + parseInt(tile.id, 10);
newCollection.addImage(gid, image);
}
imageCollections.push(newCollection);
I've tested it and it worked, now my Phaser objects have the correct image, taking into account the non-consecutive ids that the Tileset may have.
EDIT: Here's a link to a description as of why TILED work this way.
mapeditor/tiled#1233
mapeditor/tiled#1118
Version
Description
I'm building a 2d platformer, making heavy use of TILED's Object Layers. I found that when I delete an object from a Tileset (Collection of Images), TILED does not re-use the internal id of the deleted object. So when developing a game, one usually add objects to a tileset, removes them, moves them to another Tileset that makes better sense to have it in, etc. That might left us with Tilesets with internal id's that are not consecutive, for example:
In that .tsx file (TILED external tilesets files) we see that the tile id's 0,1, 7, 8, 9 and 11 are missing, they were deleted by me in the process of building the levels. The tile id's are not consecutive: (2, 3, 4, 5, 6, 10, 12) instead of (0, 1, 2, 3, 4, 5, 6).
This is ok within TILED, cause the internal gid is calculated as firstgid + internal id of a tile in a tileset.
I saw that when I deleted some object from a tileset in TILED, Phaser would show me objects (declared within that Tileset/ImageCollection) with different images than they were supposed to have.
I tracked the problem to be within Phaser.Tilemaps.Parsers.Tiled.ParseTilesets
Currently it has :
stringID is the index within the array of tiles, NOT the id of the tile.
It should be something like this:
I've tested it and it worked, now my Phaser objects have the correct image, taking into account the non-consecutive ids that the Tileset may have.
EDIT: Here's a link to a description as of why TILED work this way.
mapeditor/tiled#1233
mapeditor/tiled#1118