Monogame help importing Tiled
You don't have to replace anything in tmx file. Just do not create tiles with id 0.
Right now you're creating new Tile for every cell. Change that, check first if tile id is 0, and leave cell empty if you see zero id. Check also your drawing function, since you will have null pointers in field's array of tiles.
Alternatively, check tile id before adding it to sprite batch, and skip that sprite if its id is zero. But I'd prefer not creating Tile objects in the first place.
Also take note that you might need to index individual tiles starting with 1. Actually, see `firstgid` field of tileset description structure in the map.
You don't have to replace anything in tmx file. Just do not create tiles with id 0.
Right now you're creating new Tile for every cell. Change that, check first if tile id is 0, and leave cell empty if you see zero id. Check also your drawing function, since you will have null pointers in field's array of tiles.
Alternatively, check tile id before adding it to sprite batch, and skip that sprite if its id is zero. But I'd prefer not creating Tile objects in the first place.
Hi vstrakh, thanks for the reply!
I'm struggling to figure out how to do this as right now i'm just making as many tiles as required to fill the map through:
Tile[,] tiles = new Tile[mapWidth, mapHeight]
If i check within my loop to see if is equal to 0 and then don't create a tile for that specific index, i then get an error with my drawing loop:
foreach (Tile t in tileset)
{
t.Draw(spriteBatch);
}
Update the loop that creates tiles. Instead of this:
tiles[x, y] = new Tile(new Vector2(x * 32, y * 32),
sourceTex,
new Rectangle((int)sourcePosition[intID[x, y]].X, (int)sourcePosition[intID[x, y]].Y, 32, 32)
);
Do this:
if(intID[x,y] != 0)
{
tiles[x, y] = new Tile(new Vector2(x * 32, y * 32),
sourceTex,
new Rectangle((int)sourcePosition[intID[x, y]].X, (int)sourcePosition[intID[x, y]].Y, 32, 32)
);
}
And when batching sprites for drawing, check tile reference for null:
foreach (Tile t in tileset)
{
if( t != null )
t.Draw(spriteBatch);
}