【发布时间】:2014-01-12 12:32:30
【问题描述】:
我是 xna 编程的新手,我需要一些帮助...
我构建了一个 2D 游戏,具有静态背景 在我当前的屏幕中,有背景图像,左侧有 4 个大图像,我需要在右侧绘制 16 个小图像(或多或少 50x50)
所以这是我更新左侧16张图片位置的方法 更新只调用一次
private void letterLblsLocation()
{
if (letterLbls.Count != 0)
{
letterlblposition = new Vector2(0f, 0f);
lblvector = new Vector2(0f, 0f);
int col = 1;
int lblsize = ScreenManager.GraphicsDevice.Viewport.Height / 15;
for (int lbl = 0; lbl < letterLbls.Count; lbl++)
{
letterlblposition.X = ScreenManager.GraphicsDevice.Viewport.Width - (col * lblsize);
letterlblposition.Y += 5 + lblsize;
if (letterlblposition.Y > ScreenManager.GraphicsDevice.Viewport.Height - lblsize)
{
col = col + 3;
letterlblposition.Y = 5 + lblsize;
}
recsOnRight.Add(new Rectangle((int)letterlblposition.X, (int)letterlblposition.Y, lblsize, lblsize));
lblvector.X = recsOnRight[lbl].X + 5;
lblvector.Y = recsOnRight[lbl].Y;
letterLbls[lbl].Position = lblvector;
}
}
}
这是我的更新方法
public override void Update(GameTime gameTime, bool otherScreenHasFocus,bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, false);
// Gradually fade in or out depending on whether we are covered by the pause screen.
if (coveredByOtherScreen)
pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
else
pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
if (IsActive)
{
if(questionNeeded)
{
ChooseWord();
ChooseLettersOnRight();
LabelsWithLetters();
letterLblsLocation();
}
}
}
这是我的 Draw 方法
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle(0, 0, ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height), Color.White);
if (!questionNeeded)
{
for (int i = 0; i < 16; i++)
{
if (i < 4)
{
Texture2D helpTexture = questionToDisplay.pic[i];
Rectangle helpRect = questionToDisplay.picRecs[i];
spriteBatch.Draw(helpTexture, helpRect, Color.White);
}
spriteBatch.Draw(labelSquare, recsOnRight[i], Color.White);
}
}
spriteBatch.End();
现在我的问题是,当我尝试在右侧绘制 16 个 spritebatch 时,游戏变慢了 有没有办法只绘制一次 16 张图片,然后仅在玩家点击其中一张时重新绘制它们? 或者一种合并它们以绘制一个大图像而不是 16 个小图像的方法? 或者由于我在这里的经验很少,所以这段代码中是否有什么必须更改的内容,导致我在绘制这张照片时游戏运行缓慢?
提前致谢
【问题讨论】:
-
16 个精灵甚至不应该有明显的性能影响。除非他们的分辨率非常高。你确定问题是由绘制代码引起的吗?
-
Nico,如果我删除了这个 for 循环,那么速度会再次恢复正常......所以这就是为什么认为问题出在 Draw 中,至于分辨率我尝试加载的图片有一个简单的蓝色方块 100x100 像素