2012-09-26 69 views
0

我triyng使自動類型瓷磚系統根據我的紋理地圖繪製我的int [,]瓷磚地圖,但是當我運行代碼時,我得到了整個事物橫向和紋理不出現,唯一的方法是我知道它是橫向的,因爲用於繪製紋理的「顏色」與ClearScreen的顏色不同。瓷磚系統和瓷磚圖

瓷磚排序類別:

public Texture2D Texture { get; set; } 
    public int Rows { get; set; } 
    public int Columns { get; set; } 
    public int totalTiles; 
    public int _tileWidth; 
    public int _tileHeight; 
    public int[] tiles; 
    public int[,] tileMap = new int[,] { 
      {0,0,0,0,0,0,2,0,0,0,}, 
      {0,0,0,0,0,2,2,0,0,0,}, 
      {0,0,0,0,0,2,0,0,0,0,}, 
      {0,0,0,0,0,2,0,0,0,0,}, 
      {0,0,0,2,2,2,0,0,0,0,}, 
      {0,0,0,2,0,0,0,0,0,0,}, 
      {0,0,0,2,0,0,0,0,0,0,}, 
      {0,0,2,2,0,0,0,0,0,0,}, 
      {0,0,2,0,0,0,0,0,0,0,}, 
      {0,0,2,0,0,0,0,0,0,0,} 
    }; 

    public Tile(Texture2D texture, int tileWidth, int tileHeight) 
    { 
     _tileWidth = tileWidth; 
     _tileHeight = tileHeight; 
     Texture = texture; 
     totalTiles = Rows * Columns; 

    } 


    public void Update() { 

    } 

    public void Draw(SpriteBatch spriteBatch, Vector2 location) 
    { 

     for (int row = 0; row < tileMap.GetLength(1); row++) { 

      for (int col = 0; col < tileMap.GetLength(0); col++) { 

       switch (tileMap[row, col]) { 
        case 0: 
         spriteBatch.Draw(Texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(1 * _tileWidth, 1 * _tileHeight, _tileWidth, _tileHeight), Color.Transparent); 
         break; 
        case 2: 
         spriteBatch.Draw(Texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(2 * _tileWidth, 2 * _tileHeight, _tileWidth, _tileHeight), Color.Transparent); 
         break; 
        default: 
         break; 
       } 

      } 

主類

public class Main : Microsoft.Xna.Framework.Game { 
    // Basic code for drawing. 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Tile test; 
    Texture2D texturePack; 

    public Main() { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 


    protected override void Initialize() { 

     base.Initialize(); 
    } 


    protected override void LoadContent() { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     texturePack = Content.Load<Texture2D>("AxiomTileSheet"); 
     test = new Tile(texturePack, 8, 8); 
    } 


    protected override void UnloadContent() { 
    } 


    protected override void Update(GameTime gameTime) { 

     if (Keyboard.GetState().IsKeyDown(Keys.Escape)) 
      this.Exit(); 


     base.Update(gameTime); 
    } 


    protected override void Draw(GameTime gameTime) { 
     GraphicsDevice.Clear(Color.White); 

     spriteBatch.Begin(); 
     test.Draw(spriteBatch); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

回答

2

switch (tileMap[row, col]) 

應該是

switch (tileMap[col, row]) 

,因爲您使用col來表示x值,而使用y來表示行。

您還需要在矩形計算中將其切換。

根據我的經驗(如Andrew提到的,這可能並不是每個案例都值得)的性能的另一個暗示,而不是每個框架創建新的矩形定義2矩形在您瓷磚類,只是改變每幀的x和y值

floAr

編輯:色差問題=============================

你可以完全地擺脫的開關盒結構使用

​​

注:_tH = _tileHeight,_tW = _tileWidth 我測試了這一點,它的工作對我來說就像魅力;) 如果你想保持開關的情況下確保你設置Color.White作爲着色顏色(最後spritebatch參數)。您需要更正開關盒的值,因爲您在地圖定義中使用0和2,但在開關盒中使用0和1。將案例擴大到3個(因爲現在看起來你有4個瓷磚)也適用於我。

所以我認爲你的問題是基於錯誤的顏色和case結構錯字。

希望這有助於:)

+0

這不是一個有價值的優化。 'Rectangle'是一個值類型。創建一個是非常便宜的操作。 (引用類型是昂貴的,由於垃圾創建。) –

+0

好的謝謝你的輸入。我編輯了答案來反映它。這個技巧是根據個人經驗給出的。在幾年前的一個大學項目中,這給了我們更多的5 FPS,但我們也有很多這些繪製調用。 – floAr

+1

它理論上可以提高一小部分的性能([這是一個有趣的案例,我正在調查一次](http://gamedev.stackexchange.com/questions/604/why-does-farseer-2-x-store-temporaries -as-構件和 - 不 - 上的堆疊網))。然而這是非常微觀的優化。如果可能的話,最好避免 - 並且只能用適當的分析和需要來完成。不是很好的一般建議 - 這就是爲什麼我跳上它。 (而參考型病例是一個可接受的系統優化 - 您試圖避免分配到任何地方,以避免昂貴的收集。) –