Drawing a layer

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

I have noticed that using Squared.Tiled layers are drawn in alphabetical order, so it often appears that your layers aren't drawn. Your object layers will be drawn within this alphabetical order also. You could fix alternatively draw your layers separately:

Draw a Tiled layer

Quite often, not all layers are drawn. For example the top down racing game I created seems to have 2 layers which are drawn but has a layer that doesn't. So instead you can create a Layer in Game1.cs, read the Layer from the map and then draw the Layer separately in the draw method.

In the declaration section of your Game1.cs you can declare a Layer:

Layer testLayer;

you can then add the following in the LoadContent method to read in the layer:

testLayer = map.Layers["LayerName"];

Now in the Draw method, and you must put this code after the code to draw your map:

testLayer.Draw(spriteBatch, 
    map.Tilesets.Values, 
    new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight),
    new Vector2(300,300),
    128, 
    128);

The map.Tilesets.Values passes the tileset into the layers draw method. The rectangle should be how much of the map is displayed, The Vector2 is the position in the map, these should obviously match the position & viewport specified in the code to draw the main map. 128 is the dimensions of each tile, you could get this directly from the map so:

testLayer.Draw(spriteBatch, 
    map.Tilesets.Values, 
    new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight),
    new Vector2(300,300),
    map.TileWidth, 
    map.TileHeight);

Drawing the map layer by layer

You could alternatively in the draw method of Game1.cs, remove your map.Draw() line and instead do something like this to select each layer in turn:

foreach (Layer testlayer in Map.Layers.Values)
{
    if (testlayer.Name != "Collision")
    {
        testLayer.Draw(spriteBatch, 
            map.Tilesets.Values, 
            new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight),
            new Vector2(300,300),
            map.TileWidth, 
            map.TileHeight);
    }
}

The above example will not draw the layer called Collision. Remember the layers will be drawn in alphabetical order.

Drawing Objects

The layer foreach loop above will not draw any objects. You can draw the objects within an object layer or object group by doing the following, but the easiest approach is to name your layers alphabetically including your object layers:

foreach (Squared.Tiled.Object test in Map.ObjectGroups["Objects"].Objects.Values)
{
    if (test.Texture != null)
    {
        test.Draw(
            spriteBatch, 
            new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), //window
            new Vector2(0,0), //Offset
            new Vector2(300,300), //Position
            1f //float for opacity
            );
    }
}