I am new to xna programming I need a little help here...
I build a 2D game, with static background In my current screen there are the background image, 4 large images on the left and i need to draw 16 small images (50x50 more or less) on the right
So this is my method to update the location of the 16 images on the left It is called only once by the Update
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;
}
}
}
This is my Update method
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();
}
}
}
And this is my Draw method
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();
Now my problem is that when I try to draw the 16 spritebatch on right the game becomes slow Is there a way to draw the 16 pics only once and redraw them only when the player clicks on one of them? Or a way to merge them in order to draw one large image and not 16 smaller? Or since I have a very little experience here, is there anything in this code that must change that makes the game runs slow when I draw this pics?
Thanks in advance