我是新来的XNA编程我在这里需要一些帮助......XNA绘制spritebatch只有一次
我建立一个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;
}
}
}
Th是我更新方法
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();
}
}
}
这是我的绘制方法
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个精灵甚至不应该有明显的性能影响。除非他们的分辨率非常高。你确定问题是由绘制代码造成的吗? –
尼科如果我删除此循环,然后速度又恢复正常...所以这就是为什么认为问题出现在Draw中,至于图片我试图加载的分辨率是一个简单的蓝色方块100x100像素 – nansyg21