Hi-- this is my first post here, so please tell me if I do anything wrong. So, I’m attempting to use TiledSharp to import my tile maps made in Tiled. I’m using the example provided by Temeez here: https://github.com/Temeez/TiledSharp-MonoGame-Example . However, I’m getting the error “ITmxLayer does not contain a definition for ‘Tiles’ and no acceptable extension method ‘Tiles’ accepting a first argument of the type ‘ITmxLayer’ could be found.” Can anyone either spot my error or let me know if they’ve had a similar issue? Thanks!
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using Nez;
using TiledSharp;
namespace _00monogame_test
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Nez.Core
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//mapstuff
TmxMap easyMap;
Texture2D easyTileset;
//tile properties
int tileWidth;
int tileHeight;
int tilesetTilesWide;
int tilesetTilesHigh;
//easyMap = new TmxMap("testmaps/easy_test_map.tmx");
//map properties
//TmxTileset easyTileset = easyMap.Tilesets["testmaps/testspritesheet.tsx"];
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 640;
graphics.PreferredBackBufferHeight = 480;
Content.RootDirectory = "Content";
//position = new Vector2(0, 0);
this.IsFixedTimeStep = true;
this.graphics.SynchronizeWithVerticalRetrace = true;
this.TargetElapsedTime = new System.TimeSpan(0, 0, 0, 0, 33); //33 ms = 30 fps
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
//based on Temeez example
easyMap = new TmxMap("testmaps/easy_test_map.tmx");
easyTileset = Content.Load<Texture2D>(easyMap.Tilesets[0].Name.ToString());
tileWidth = easyMap.Tilesets[0].TileWidth;
tileHeight = easyMap.Tilesets[0].TileHeight;
tilesetTilesWide = easyTileset.Width / tileWidth;
tilesetTilesHigh = easyTileset.Height / tileHeight;
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
for (var i = 0; i < easyMap.Layers[0].Tiles.Count; i++)
{
int gid = easyMap.Layers[0].Tiles[i].Gid;
// Empty tile, do nothing
if (gid == 0)
{
}
else
{
int tileFrame = gid - 1;
int column = tileFrame % tilesetTilesWide;
int row = (int)Math.Floor((double)tileFrame / (double)tilesetTilesWide);
float x = (i % easyMap.Width) * easyMap.TileWidth;
float y = (float)Math.Floor(i / (double)easyMap.Width) * easyMap.TileHeight;
Rectangle tilesetRec = new Rectangle(tileWidth * column, tileHeight * row, tileWidth, tileHeight);
spriteBatch.Draw(easyTileset, new Rectangle((int)x, (int)y, tileWidth, tileHeight), tilesetRec, Color.White);
}
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}