Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
C# 使用XNA和C在X轴和Y轴上的二维视差背景#_C#_Background_Xna_2d_Parallax - Fatal编程技术网

C# 使用XNA和C在X轴和Y轴上的二维视差背景#

C# 使用XNA和C在X轴和Y轴上的二维视差背景#,c#,background,xna,2d,parallax,C#,Background,Xna,2d,Parallax,我正在使用XNA开发一款太空射击游戏,并遵循多个教程创建了视差背景。到目前为止,我可以让它沿着一个轴,X轴或Y轴,但不能同时沿着两个轴。我有一个跟随玩家的摄影机类,玩家移动它(而不是“世界”),因为我认为只移动玩家比移动玩家周围的所有东西更容易 到目前为止,背景跟不上玩家的节奏,也不能同时理解两个轴。我想用一个平铺引擎,但它不能让我对不同的图层产生视差,是吗 有谁能帮我理解我需要做什么,或者推荐一个可以同时做两个axis的教程吗?这次我似乎无法独自找到答案 下面是我的后台类的代码: class

我正在使用XNA开发一款太空射击游戏,并遵循多个教程创建了视差背景。到目前为止,我可以让它沿着一个轴,X轴或Y轴,但不能同时沿着两个轴。我有一个跟随玩家的摄影机类,玩家移动它(而不是“世界”),因为我认为只移动玩家比移动玩家周围的所有东西更容易

到目前为止,背景跟不上玩家的节奏,也不能同时理解两个轴。我想用一个平铺引擎,但它不能让我对不同的图层产生视差,是吗

有谁能帮我理解我需要做什么,或者推荐一个可以同时做两个axis的教程吗?这次我似乎无法独自找到答案

下面是我的后台类的代码:

class Background
{
    // Textures to hold the two background images
    Texture2D spaceBackground, starsParallax;

    int backgroundWidth = 2048;
    int backgroundHeight = 2048;

    int parallaxWidth = 2048;
    int parallaxHeight = 2048;

    int backgroundWidthOffset;
    int backgroundHeightOffset;

    int parallaxWidthOffset;
    int parallaxHeightOffset;

    public int BackgroundWidthOffset
    {
        get { return backgroundWidthOffset; }
        set
        {
            backgroundWidthOffset = value;
            if (backgroundWidthOffset < 0)
            {
                backgroundWidthOffset += backgroundWidth;
            }
            if (backgroundWidthOffset > backgroundWidth)
            {
                backgroundWidthOffset -= backgroundWidth;
            }
        }
    }

    public int BackgroundHeightOffset
    {
        get { return backgroundHeightOffset; }
        set
        {
            backgroundHeightOffset = value;
            if (backgroundHeightOffset < 0)
            {
                backgroundHeightOffset += backgroundHeight;
            }
            if (backgroundHeightOffset > backgroundHeight)
            {
                backgroundHeightOffset -= backgroundHeight;
            }
        }
    }

    public int ParallaxWidthOffset
    {
        get { return parallaxWidthOffset; }
        set
        {
            parallaxWidthOffset = value;
            if (parallaxWidthOffset < 0)
            {
                parallaxWidthOffset += parallaxWidth;
            }
            if (parallaxWidthOffset > parallaxWidth)
            {
                parallaxWidthOffset -= parallaxWidth;
            }
        }
    }

    public int ParallaxHeightOffset
    {
        get { return parallaxHeightOffset; }
        set
        {
            parallaxHeightOffset = value;
            if (parallaxHeightOffset < 0)
            {
                parallaxHeightOffset += parallaxHeight;
            }
            if (parallaxHeightOffset > parallaxHeight)
            {
                parallaxHeightOffset -= parallaxHeight;
            }
        }
    }

    // Constructor when passed a Content Manager and two strings
    public Background(ContentManager content,
                      string sBackground, string sParallax)
    {
        spaceBackground = content.Load<Texture2D>(sBackground);
        backgroundWidth = spaceBackground.Width;
        backgroundHeight = spaceBackground.Height;

        starsParallax = content.Load<Texture2D>(sParallax);
        parallaxWidth = starsParallax.Width;
        parallaxHeight = starsParallax.Height;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        // Draw the background panel, offset by the player's location
        spriteBatch.Draw(
            spaceBackground,
            new Rectangle(-1 * backgroundWidthOffset,
                          -1 * backgroundHeightOffset, 
                          backgroundWidth,
                          backgroundHeight),
            Color.White);

        // If the right edge of the background panel will end 
        // within the bounds of the display, draw a second copy 
        // of the background at that location.
        if (backgroundWidthOffset > backgroundWidth)
        {
            spriteBatch.Draw(
                spaceBackground,
                new Rectangle(
                  (-1 * backgroundWidthOffset) + backgroundWidth, 0,
                  backgroundWidth, backgroundHeight),
                Color.White);
        }
        else //(backgroundHeightOffset > backgroundHeight - viewportHeight)
        {
            spriteBatch.Draw(
                spaceBackground,
                new Rectangle(
                  0, (-1 * backgroundHeightOffset) + backgroundHeight,
                  backgroundHeight, backgroundHeight),
                Color.White);
        }

        // Draw the parallax star field
        spriteBatch.Draw(
            starsParallax,
            new Rectangle(-1 * parallaxWidthOffset,
                            0, parallaxWidth,
                            parallaxHeight),
            Color.SlateGray);
        // if the player is past the point where the star 
        // field will end on the active screen we need 
        // to draw a second copy of it to cover the 
        // remaining screen area.
        if (parallaxWidthOffset > parallaxWidth)
        {
            spriteBatch.Draw(
                starsParallax,
                new Rectangle(
                    (-1 * parallaxWidthOffset) + parallaxWidth,
                    0,
                    parallaxWidth,
                    parallaxHeight),
                Color.White);
        }
    }
}

视觉上,背景有点跳跃,似乎随机跳过或重叠背景瓷砖。

我过去使用过这种方法,效果很好:


它使用着色器来实现效果,从而实现了快速实现

有一个完整的例子

谢谢,这很容易理解和实现。我只是用透明的背景来制作视差。工作完美!我无法修复断开的链接,这不是我的网站。断开的链接。。。除非你只是想为微软做广告:/下载pdf并搜索“Platformer:添加滚动级别”,我不认为当用户要求XNA时指向微软的链接是免费的和断章取义的广告…我是在开玩笑说微软的广告。。。感谢4年后的跟进。干杯无论如何你说得对,链接断了。这个链接以前是一个html站点,包含xna文档而不是pdf。很高兴看到MS至少以某种方式保留了这些信息。
background.BackgroundWidthOffset -= 2;
background.ParallaxWidthOffset -= 1;