2012-05-29 55 views
3

我正在學習XNA,但在將精靈放置在瓷磚地圖上時有一些缺陷。在瓷磚地圖上顯示精靈XNA

這裏是香港專業教育學院的嘗試:

public void Draw(SpriteBatch spriteBatch,int tileWidth,int tileHeight) 
    { 
     spriteBatch.Draw(texture , position , Color.White); 
    }// This code draws the sprite but the sprite is not on the tile map but outside it. 


public void Draw(SpriteBatch spriteBatch,int tileWidth,int tileHeight) 
    { 
     spriteBatch.Draw(texture , new Rectangle((int)position.X * tileWidth , (int)position.Y * tileHeight , tileWidth , tileHeight) , Color.White); 
    }// And this code does nothing, doesnt even draw the sprite 

這是二維數組:

int[ , ] map = { 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
         {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
         {0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
         {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
         {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
         {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
        }; 

,其中0 =牆上& 1 =通路

請告訴我或告訴我如何去繪製只在地圖上的精靈?

感謝

編輯:我遇到的問題是,精靈(上底部的黑色方形右)如果向左移動,變褐色後面(這是瓦圖的PRT)。我如何讓這個它移動的瓦片地圖上面,而不是下面

In game picture

+0

相似,但任天堂DS做了什麼。使用一個自定義的字體文件,但用精靈替換字符,所以'X'將是一堵牆,一個'Z'將成爲敵人。你會想循環遍歷數組繪製你的精靈,在哪裏'i = X'等...... – dtsg

+0

@duane嗨,請檢查我的編輯 –

+2

你可以通過在繪製前設置'SpriteSortMode'來阻止這種情況發生 – dtsg

回答