2016-09-27 62 views
0

我有這個問題瓷磚只會繪製一個特定的紋理。過去幾天我一直在尋找瓷磚引擎,並決定爲了教育目的而自己動手。我設法在屏幕上只顯示一個圖塊,並在地圖上繪製該圖塊。在地圖中它將所有的圖塊視爲相同的紋理,而我希望能夠確定哪些圖塊在哪裏。例如,1 =草,2 =天空,3 =污垢等。我一直在這裏和那裏的教程,並在網上搜索嘗試找到這個問題,但無濟於事。 在TileMap類的LoadContent中,Console.WriteLine狀態有3個紋理加載到列表中。 我的印象是,tileMap中的繪製方法會將索引設置爲與tile貼圖對應的貼圖ID,然後在遊戲中的地圖上繪製該特定貼圖。例如,索引1將等於丟失的第一個紋理,索引2將等於第二個紋理,依此類推。 這是一個正確的假設還是我的方式?只有一個瓷磚繪製在地圖xna c#

另外,你可以給我任何幫助/指示方法來解決這個問題,只有一個紋理顯示在屏幕上。

預先感謝您。

瓷磚地圖

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

      } 
     } 
    } 
} 

地圖細胞

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

回答

1
class MapCell 
{ 
    public int TileID { get; set; } 

    public MapCell(int tileID) 
    { 
     tileID = TileID; 
    } 
} 

應改爲TileID = tileID。就像您將3傳遞給tileID,然後將0(TileID的默認值)分配給tileID一樣。但TileID永遠不會改變,所以它始終是0.交換它們,它可能會工作。