简体   繁体   English

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

[英]XNA C# 2D Scrolling Background 3 or More

I've trying to create a scrolling background with 3 background, but everything When the 3rd start to come out. 我正在尝试创建具有3个背景的滚动背景,但是当第3个开始出现时,一切都应如此。 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. 它在第3个屏幕的前面创建了一个巨大的蓝屏(游戏的默认背景),而在3RD之后则不显示任何背景。 I have no idea how fix this, and I already know it something simple. 我不知道该如何解决,而且我已经知道一些简单的方法。 I've got the 3RD one to work, but . 我有一个3RD可以工作,但是。

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 pos的Vector2代码是

        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. 我认为您不希望嵌套if语句。 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. 您发布的代码会一直将第二个背景移至-950,直到第一个背景移至950为止。由于第一个背景不断移回至-950,因此它永远不会移至950以上,因此它永远不会进入最后一个。 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. XNA的Vector2类将x和y分量存储为浮点数,而C#中的浮点数最大值在3.4e38左右,根据MSDN精确到7位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM