Using a tmx map in monogame

From TRCCompSci - AQA Computer Science
Revision as of 15:02, 23 September 2017 by Admin (talk | contribs) (Created page with "=Install TiledSharp= The easiest method is to create your MonoGame project then: # Click on project & select Nuget Package Manager # Search online for TiledSharp & Install # I...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Install TiledSharp

The easiest method is to create your MonoGame project then:

  1. Click on project & select Nuget Package Manager
  2. Search online for TiledSharp & Install
  3. In the Game1.cs of your project add
    using TiledSharp;
    

Declarations

Add the following declaration within the main class in Game1.cs:

        TmxMap map;
        Texture2D tileset;

        int tileWidth;
        int tileHeight;
        int tilesetTilesWide;
        int tilesetTilesHigh;

Load Content

in the LoadContent method add the following to load your map & tiles, it also sets the size of the tiles:

            map = new TmxMap("Content/exampleMap.tmx");
            tileset = Content.Load<Texture2D>(map.Tilesets[0].Name.ToString());

            tileWidth = map.Tilesets[0].TileWidth;
            tileHeight = map.Tilesets[0].TileHeight;

            tilesetTilesWide = tileset.Width / tileWidth;
            tilesetTilesHigh = tileset.Height / tileHeight;