我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);
}
}
這不是一個有價值的優化。 'Rectangle'是一個值類型。創建一個是非常便宜的操作。 (引用類型是昂貴的,由於垃圾創建。) –
好的謝謝你的輸入。我編輯了答案來反映它。這個技巧是根據個人經驗給出的。在幾年前的一個大學項目中,這給了我們更多的5 FPS,但我們也有很多這些繪製調用。 – floAr
它理論上可以提高一小部分的性能([這是一個有趣的案例,我正在調查一次](http://gamedev.stackexchange.com/questions/604/why-does-farseer-2-x-store-temporaries -as-構件和 - 不 - 上的堆疊網))。然而這是非常微觀的優化。如果可能的話,最好避免 - 並且只能用適當的分析和需要來完成。不是很好的一般建議 - 這就是爲什麼我跳上它。 (而參考型病例是一個可接受的系統優化 - 您試圖避免分配到任何地方,以避免昂貴的收集。) –