I'm having a bit of trouble with my code meant to create a scrolling background of random-ish ones and zeros. When moving to the right (or left), it scrolls smoothly for a few seconds, stops/jerks, then starts scrolling smoothly again. I've spent an entire day now trying to iron out this issue and nothing that I've done makes it any better.
Here is my isolated code:
namespace TestScrollyNumbers
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1366;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
}
KeyboardState CKBS = new KeyboardState();
Random Randomness = new Random();
Texture2D NumberScrolly1;
Texture2D NumberScrolly2;
Texture2D NumberScrolly3;
Texture2D NumberScrolly4;
Texture2D NumberScrolly5;
float XPosScroll = 0;
float XPosScrollVel = 0;
int i = 0;
int meh = 1;
public List<Texture2D> BackgroundScrollyTextures = new List<Texture2D>();
public List<float> ScrollyNumberXPos = new List<float>(); //This list contains the final X positions for the number bars.
public List<float> ScrollyNumberXStart = new List<float>(); //This will be used to calculate the final X positions for each bar.
public List<int> ScrollyNumberMultiplier = new List<int>(); //List of Y position multipliers so that I can calculate the Ypos in the draw command.
public List<int> ScrollyNumberType = new List<int>(); //List of randomized textures for each bar.
protected override void Initialize()
{
base.Initialize();
BackgroundScrollyTextures.Add(NumberScrolly1);
BackgroundScrollyTextures.Add(NumberScrolly2);
BackgroundScrollyTextures.Add(NumberScrolly3);
BackgroundScrollyTextures.Add(NumberScrolly4);
BackgroundScrollyTextures.Add(NumberScrolly5); //Add all of the possible textures to the list of textures so that each number in the "ScrollyNumberType" list can correspond to a texture.
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
NumberScrolly1 = Content.Load<Texture2D>("NumbersForBackground1");
NumberScrolly2 = Content.Load<Texture2D>("NumbersForBackground1");
NumberScrolly3 = Content.Load<Texture2D>("NumbersForBackground1");
NumberScrolly4 = Content.Load<Texture2D>("NumbersForBackground1");
NumberScrolly5 = Content.Load<Texture2D>("NumbersForBackground1"); //Loading :P
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
CKBS = Keyboard.GetState(); //Current Key Board State
if (meh == 1)
{
for (i = 0; i < 18; i++)
{
ScrollyNumberMultiplier.Add(Randomness.Next(0, 11));
ScrollyNumberType.Add(Randomness.Next(0, 5));
ScrollyNumberXStart.Add(i * 80); //Each bar is 80 pixels wide
ScrollyNumberXPos.Add(0);
}
meh = 0;
}
if (CKBS.IsKeyDown(Keys.Right))
{
XPosScrollVel += .6f;
}
if (CKBS.IsKeyDown(Keys.Left))
{
XPosScrollVel -= .6f;
} //Scrolling - add to the velocity
XPosScrollVel *= .9f;
XPosScroll += XPosScrollVel; //Add the current X Velocity to the X Scroll
for (i = 0; i < ScrollyNumberType.Count; i++)
{
if (ScrollyNumberXPos[i] < -79)
{
ScrollyNumberXStart[i] += 1440; //Instead of creating a new bar, use the bar that just went off of the screen and give it a new Y pos value
ScrollyNumberMultiplier[i] = Randomness.Next(0, 11);
}
if (ScrollyNumberXPos[i] > 1365)
{
ScrollyNumberXStart[i] -= 1440; //Same thing as before, except reverse.
ScrollyNumberMultiplier[i] = Randomness.Next(0, 11);
}
ScrollyNumberXPos[i] = ScrollyNumberXStart[i] - XPosScroll; //final XPos = where it started, plus how many times it's gone across the screen, minus how much you've scrolled.
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
for (i = 0; i < ScrollyNumberType.Count; i++)
{
spriteBatch.Draw(BackgroundScrollyTextures[ScrollyNumberType[i]], new Vector2((int)ScrollyNumberXPos[i], (-37 - 56 * ScrollyNumberMultiplier[i])), null, Color.White, 0, new Vector2(0, 0), new Vector2(1, 1), SpriteEffects.None, 0);
} //And then draw everything.
spriteBatch.End();
base.Draw(gameTime);
}
}
}
You can use this image to test my code (I didn't include the four others as that would make this post unnecessarily long) if you don't know the answer right away:
For anyone wondering, I use Microsoft Visual C# 2010 Express and XNA Game Studio 4.0.
graphics.IsFullScreen = true;
otherwise the issue is most likely due to the GPU being shared. \$\endgroup\$