【发布时间】:2013-07-23 16:09:02
【问题描述】:
我目前正在使用 MonoGame 开发一款自上而下的游戏,该游戏使用图块来指示位置是否可步行。瓷砖的大小为 32x32(图像也有)
一个 200 x 200 的网格正在填充墙砖(并且应该使用随机生成器创建路径和房间),但是当我在屏幕上绘制所有图块时,很多图块丢失了。下图是在位置 (x81 y183) 之后根本没有绘制瓷砖的图像?
用于填充数组的代码将墙砖放在网格上,砖的位置是它的阵列位置乘以父用于相机位置的砖大小 (32x32)
public override void Fill(IResourceContainer resourceContainer)
{
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
objectGrid[i, j] = new Wall(resourceContainer);
objectGrid[i, j].Parent = this;
objectGrid[i, j].Position = new Vector2(i * TileWidth, j * TileHeight);
}
}
绘制时,我只是遍历所有图块并相应地绘制它们。这就是 Game.Draw 函数中发生的事情
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Yellow);
// TODO: Add your drawing code here
spriteBatch.Begin();
map.Draw(gameTime, spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
map.draw 函数调用这个函数,它基本上绘制每个图块。我尝试设置一个计数器来计算每个图块的绘制调用被命中的次数,每次更新绘制函数被调用 40000 次,这是我使用的图块数量。所以它把它们都画了,但我仍然没有在屏幕上看到它们
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
if (objectGrid[i, j] != null)
{
objectGrid[i, j].Draw(gameTime, spriteBatch);
}
}
}
这是绘制瓷砖的代码。当前图像始终为 0,GlobalPosition 是图块的 位置 减去 相机位置。
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
if (visible)
spriteBatch.Draw(textures[currentImage], GlobalPosition, null, color, 0f, -Center, 1f, SpriteEffects.None, 0f);
}
我为代码墙道歉。对我来说这一切看起来都很简单,但我似乎无法找出为什么它没有绘制所有的瓷砖。对于未绘制的图块 visible 仍然是 true 并且 currentImage 应该是 0
【问题讨论】:
-
SpriteBatch.Draw 调用中的 -Center 是什么?
-
中心是精灵大小的一半。这是因为精灵通常是从左上角绘制的。使用 -Center 意味着图像的中心实际上在位置本身上
-
我在您发布的代码中看不到任何明显的错误。它可能在其他地方。
-
没有比这更多的代码了。但奇怪的是,有时他画左右却漏了中间。但是中间的所有图块仍然存在,它们都可见并且都有纹理,但它们只是不显示
标签: xna drawing monogame spritebatch