I've trying to create a scrolling background with 3 background, but everything When the 3rd start to come out. It creates a giant blue screen(Default background of the game) infront of the 3rd and after the 3RD it doesn't show any background. I have no idea how fix this, and I already know it something simple. I've got the 3RD one to work, but .
Code that I'm trying to use.
public void Update(GameTime gameTime)
{
bgPos0.Y += speed;
bgPos1.Y += speed;
bgPos2.Y += speed;
if (bgPos0.Y >= 950)
{
bgPos1.Y = -950;
if (bgPos1.Y >= 950) // Doesn't go fully down.
{
bgPos2.Y = -950;
if (bgPos2.Y >= 950)
{
bgPos0.Y = 0; //(Try to change it to -950 still doesn't work. I guest it due to that bgPos0 is set to 0, 0)
}
}
}
}
And the Vector2 code for the pos are
bgPos0 = new Vector2(0, 0);
bgPos1 = new Vector2(0, -950);
bgPos2 = new Vector2(0, -1900); // Could be the large -1900 number that destroying the code. To make it not work.
So how do I fix this? I wish I could fix it right now, but I can't for some reason.
I don't think you want to have the if-statements nested. The code you posted constantly moves the second background to -950 for as long as the first is past 950. Since the first is constantly moved back to -950 it shouldn't ever manage to move past 950, and so it never gets into the last one. I think what you probably want to do is something more like:
public void Update(GameTime gameTime)
{
bgPos0.Y += speed;
if(bgPos0.Y > 950) {
bgPos0.Y = -950;
}
bgPos1.Y += speed;
if(bgPos1.Y > 950) {
bgPos1.Y = -950;
}
bgPos2.Y += speed;
if(bgPos1.Y > 950) {
bgPos1.Y = -950;
}
}
[EDIT]: As an aside, the number in question isn't nearly large enough to cause problems. XNA's Vector2 class stores the x and y components as floats, and the maximum value for a float in C# is somewhere around 3.4e38 or so, and accurate to 7 digits according to MSDN.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.