2

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

到目前为止,背景跟不上玩家,也无法同时理解两个轴。我想过一个平铺引擎,但这不会让我在不同的图层上产生视差,对吗?

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

这是我的背景类的代码:

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

然后主游戏与玩家一起移动背景。

background.BackgroundWidthOffset -= 2;
background.ParallaxWidthOffset -= 1;

从视觉上看,背景有点跳跃,似乎随机跳过或重叠背景图块。

4

2 回答 2

1

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

http://www.david-gouveia.com/scrolling-textures-with-zoom-and-rotation/

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

于 2013-04-15T11:20:13.597 回答
0

这里有一个完整的例子。

于 2013-04-15T12:10:32.673 回答