Sign in to follow this  
Mikle7

Monogame help importing Tiled

Recommended Posts

Hey guys, I need a hand getting through this at the moment. So i've got a monogame project that's very basic right now, intend to develop it into a platformer.
 
I've tried following a tutorial that allows me to create a map in Tiled and then import it into my project.
The problem is, I'm getting the tile at position 0 repeated on every tile i've left empty and i've found that this is because in my .tmx file, the empty tile is denoted with "0" which will be referring to the first tile in my tilemap. I don't want this, I want it display only the tiles I have made in Tiled and skip ones that are empty. I've tried replacing the 0's within my .tmx file with -1 but that causes out of bounds exceptions within my loop. Any help or guidance here would be greatly appreciated.
 
 
 
The method that's the problem is getTileSet()
 
 
I've attached an image of what it looks like and the tilemap i'm using.

 

Share this post


Link to post
Share on other sites

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.

Edited by vstrakh

Share this post


Link to post
Share on other sites

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);
}

Share this post


Link to post
Share on other sites

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);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this