gpt4 book ai didi

c# - xna 只绘制一次 spritebatch

转载 作者:行者123 更新时间:2023-11-30 22:10:08 26 4
gpt4 key购买 nike

我是 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();
}


}
}

这是我的绘制方法

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 个小图像的方法?或者因为我在这里的经验很少,所以这段代码中是否有任何必须更改的地方导致我绘制此图片时游戏运行缓慢?

提前致谢

最佳答案

您正在 Draw 调用期间初始化图形设备、SpriteBatch、SpriteFont 和纹理。这真的很糟糕。

将这些初始化移动到 Draw 函数之外的 LoadContent() 函数中,并且只初始化一次。

关于c# - xna 只绘制一次 spritebatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com