我正在根据我的纹理图集制作一个自动类型图块系统来绘制我的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);
}
}
答案 0 :(得分:2)
此
switch (tileMap[row, col])
应该是
switch (tileMap[col, row])
当你使用col为你x值和row为y。
您还需要在矩形计算中切换它。
根据我的经验提供性能的另一个提示(正如Andrew提到的那样,在每种情况下都不值得)而不是每帧都创建新的矩形在你的Tile类中定义2个矩形,并且每帧只更改x和y值
floAr
编辑:颜色问题=============================
您可以使用
完全摆脱switch case构造public void Draw(SpriteBatch spriteBatch)
{
for (int row = 0; row < tileMap.GetLength(1); row++) {
for (int col = 0; col < tileMap.GetLength(0); col++)
{
spriteBatch.Draw(_texture, new Rectangle(row * _tW, col * _tH, _tW, _tH), new Rectangle(tileMap[row, col] * _tW, tileMap[row, col] * _tH, _tW, _tH), Color.White);
}
}
}
注意:_tH = _tileHeight,_tW = _tileWidth 我测试了它,它对我来说就像一个魅力;) 如果要保留开关盒,请确保将Color.White设置为着色颜色(最后一个spritebatch参数)。并且您需要更正开关盒的值,因为在地图定义中使用0和2,但在开关盒中使用0和1。将案例扩展到3(因为看起来你现在有4个瓷砖)也适合我。
所以我认为你的问题是基于错误的颜色和案例结构中的拼写错误。
希望这有帮助:)