XNA Parallaxing背景不会显示所有图层

时间:2013-08-09 17:23:56

标签: c# xna camera 2d parallax

我正在使用Platformer入门套件,到目前为止,我已添加水平和垂直“相机”运动,我试图添加一个parallaxing背景的旅馆。问题是,在两个背景图层之后,它会停止显示其余图层。我是XNA的新手,需要一些帮助:)。下面是问题的图片:

layer fail

继承代码。请告诉我你是否需要更多:)

图层类:

class Layer
 {
    public Texture2D[] Textures { get; private set; }
    public float ScrollRate { get; private set; }

    public Layer(ContentManager content, string basePath, float scrollRate)
    {
        // Assumes each layer only has 3 segments.
        Textures = new Texture2D[3];
        for (int i = 0; i < 3; ++i)
            Textures[i] = content.Load<Texture2D>(basePath + "_" + i);

        ScrollRate = scrollRate;
    }

    public void Draw(SpriteBatch spriteBatch, float cameraPosition, float cameraPositionYAxis)
    {
        // Assume each segment is the same width.
        int segmentWidth = Textures[0].Width;

        // Calculate which segments to draw and how much to offset them.
        float x = cameraPosition * ScrollRate;
        float y = ScrollRate;
        int leftSegment = (int)Math.Floor(x / segmentWidth);
        int rightSegment = leftSegment + 1;
        x = (x / segmentWidth - leftSegment) * -segmentWidth;

        spriteBatch.Draw(Textures[leftSegment % Textures.Length], new Vector2(x, -y), Color.White);
        spriteBatch.Draw(Textures[rightSegment % Textures.Length], new Vector2(x + segmentWidth, -y), Color.White);
    }


}

使用我的ScrollCamera继承我的Level.cs中的draw方法(不知道ScrollCamera是否与它有任何关系)

public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        ScrollCamera(spriteBatch.GraphicsDevice.Viewport);
        Matrix cameraTransformYAxis = Matrix.CreateTranslation(-cameraPosition, -cameraPositionYAxis, 0.0f);
        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp,
            DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, cameraTransformYAxis);

        //added this foreach loop 
        foreach (var layer in layers)
        {
            layer.Draw(spriteBatch, cameraPosition, cameraPositionYAxis);
        }

        DrawTiles(spriteBatch);
        Player.Draw(gameTime, spriteBatch);
        foreach (Enemy enemy in enemies)
        {
            enemy.Draw(gameTime, spriteBatch);
        }

        spriteBatch.End();

    }

    private void ScrollCamera(Viewport viewport)
    {
        #if ZUNE
        const float ViewMargin = 0.4f;
        #else
        const float ViewMargin = 0.5f;
        #endif

        float marginWidth = viewport.Width * ViewMargin;
        float marginLeft = cameraPosition + marginWidth;
        float marginRight = cameraPosition + viewport.Width - marginWidth;


        const float TopMargin = 0.4f;
        const float BottomMargin = 0.4f;
        float marginTop = cameraPositionYAxis + viewport.Height * TopMargin;
        float marginBottom = cameraPositionYAxis + viewport.Height - viewport.Height * BottomMargin;
        //    float maxCameraPositionYOffset = Tile.Height * Height - viewport.Height; 


        float CameraMovement = 0.0f;
        if (Player.Position.X < marginLeft)
            CameraMovement = Player.Position.X - marginLeft;
        else if (Player.Position.X > marginRight)
            CameraMovement = Player.Position.X - marginRight;
        //Aktualizuj przesuwanie ekranu, ale zapobiegnij wyjściu poza mape
        float maxCameraPosition = Tile.Width * Width - viewport.Width;
        cameraPosition = MathHelper.Clamp(cameraPosition + CameraMovement, 0.0f, maxCameraPosition);



        float cameraMovementY = 0.0f;
        if (Player.Position.Y < marginTop) //above the top margin
            cameraMovementY = Player.Position.Y - marginTop;
        else if (Player.Position.Y > marginBottom) //below the bottom margin
            cameraMovementY = Player.Position.Y - marginBottom;

        float maxCameraPositionYOffset = Tile.Height * Height - viewport.Height;
        cameraPositionYAxis = MathHelper.Clamp(cameraPositionYAxis + cameraMovementY, 0.0f, maxCameraPositionYOffset);

    }

我认为就是这样。如果您需要更多代码,请告诉我:)

1 个答案:

答案 0 :(得分:1)

您想使用线性包装。 There's an excellent blog post on it right here.这当然假设你的纹理瓷砖很完美。您只需设置线性包装模式,代码示例如下:

// Use this one instead!
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
spriteBatch.Draw(texture, position, new Rectangle(-scrollX, -scrollY, texture.Width, texture.Height), Color.White);
spriteBatch.End();