我正在尝试制作一个自动类型的瓷砖系统来根据我的 Texture Atlas 绘制我的 int[,] tileMap 但是当我运行代码时,我得到了整个东西并且纹理没有出现,我知道它的唯一方法是横向是因为用于绘制纹理的“颜色颜色”与 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);
}
}