Reputation: 25
I am having this problem where the tiles will only draw one specific texture. I have been looking at tile engines for the past few days and decided to have a go at my own one for educational purposes. I have managed to get only one tile on the screen and it draws that tile in the map. in the map it treats all of the tiles as the same texture, whereas I want to be able to determine what tile goes where. For example, 1 = grass, 2 = sky, 3 = dirt etc. I have been following tutorials here and there and searching online to try and find this problem but to no avail. In the LoadContent of the the TileMap class the Console.WriteLine states there are 3 textures loaded into the list. I was under the impression that the draw method in the tileMap would set the index to the tile ID corresponding to a tile texture and then that specific tile would be drawn on the map in game. for example, index 1 would equal the first texture in the lost, index 2 would equal the second texture and so on. Is this a correct assumption or am I way off?
Also, can you give me any help/pointers on ways to fix this issue of only one texture showing on the screen.
Thank you in advance.
Tile Map
class TileMap
{
private MapCell[,] mapCell;
public const int TILE_WIDTH = 64;
public const int TILE_HEIGHT = 64;
private List<Texture2D> tileList = new List<Texture2D>();
public TileMap(int[,] exisitingMap)
{
//initialise this to a new multidimensional array;
mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
// x always starts on one
for (int x = 0; x < mapCell.GetLength(1); x++)
{
for (int y = 0; y < mapCell.GetLength(0); y++)
{
mapCell[y, x] = new MapCell(exisitingMap[y, x]);
}
}
}
public void loadTextureFiles(ContentManager content, params string[] fileNames)
{
Texture2D tileTexture;
foreach (string fileName in fileNames)
{
tileTexture = content.Load<Texture2D>(fileName);
tileList.Add(tileTexture);
Console.WriteLine(tileList.Count + " Tile texture count ");
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < mapCell.GetLength(1); x++)
{
for (int y = 0; y < mapCell.GetLength(0); y++)
{
// setting the index to the tile ID
int index = mapCell[y,x].TileID;
Texture2D texture = tileList[index];
spriteBatch.Draw(texture, new Rectangle( x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
}
}
}
}
Map Cell
class MapCell
{
public int TileID { get; set; }
public MapCell(int tileID)
{
tileID = TileID;
}
}
Game1
TileMap tileMap = new TileMap(new int[,]
{
{ 0,0,0,0,0,0,0 },
{ 1,1,1,1,1,1,1 },
{ 0,1,1,2,1,1,1 },
{ 0,2,1,3,1,3,0 },
{ 0,3,0,3,0,0,0 },
{ 0,0,0,2,0,0,0 },
{ 0,0,0,1,0,1,0 },
});
protected override void LoadContent()
{
new SpriteBatch(GraphicsDevice);
tileMap.loadTextureFiles(Content, "Tiles/Tile1", "Tiles/sky", "Tiles/dirt" );
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
tileMap.Draw(spriteBatch);
base.Draw(gameTime);
spriteBatch.End();
}
Upvotes: 0
Views: 242
Reputation: 330
class MapCell
{
public int TileID { get; set; }
public MapCell(int tileID)
{
tileID = TileID;
}
}
should be TileID = tileID
instead. As it is if you're passing in 3
to tileID, and then assigning 0 (the default value for TileID) to tileID. but TileID is never changed, so it's always 0. Swap those around and it'll probably work.
Upvotes: 1