Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XNA C#2D滚动背景3或更多_C#_Xna - Fatal编程技术网

XNA C#2D滚动背景3或更多

XNA C#2D滚动背景3或更多,c#,xna,C#,Xna,我试着用3个背景创建一个滚动背景,但是当第3个开始出现时,所有内容都会显示出来。它在第三个屏幕前面创建了一个巨大的蓝色屏幕(游戏的默认背景),在第三个屏幕之后,它不显示任何背景。我不知道如何解决这个问题,我已经知道了一些简单的事情。我还有第三个要工作,但是 我正在尝试使用的代码 public void Update(GameTime gameTime) { bgPos0.Y += speed; bgPos1.Y += speed; bgP

我试着用3个背景创建一个滚动背景,但是当第3个开始出现时,所有内容都会显示出来。它在第三个屏幕前面创建了一个巨大的蓝色屏幕(游戏的默认背景),在第三个屏幕之后,它不显示任何背景。我不知道如何解决这个问题,我已经知道了一些简单的事情。我还有第三个要工作,但是

我正在尝试使用的代码

 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)
                }
            }
        }
    }
pos的矢量2代码为

        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.

那么我该如何解决这个问题呢?我希望现在就可以修复它,但由于某些原因我不能。

我认为您不想嵌套if语句。只要第一个背景超过950,您发布的代码就会不断将第二个背景移动到-950。因为第一个不断地移回-950,所以它不应该设法移过950,所以它永远不会进入最后一个。我想你可能想做的是:

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;
    }

}
[编辑]:顺便说一下,这个数字还不足以引起问题。XNA的Vector2类将x和y分量存储为浮点数,C#中浮点数的最大值约为3.4e38,根据MSDN,精确到7位