1

我正在玩 Platformer Starter Kit,到目前为止,我已经添加了水平和垂直的“相机”运动,并且我试图添加客栈一个视差背景。问题是在两个背景层之后它会停止显示其余的层。我对 XNA 很陌生,需要一点帮助:)。这是问题的图片:

层失败

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

图层类:

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


}

这是我的Level.cs中的draw方法和我的ScrollCamera(不知道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);

    }

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

4

1 回答 1

1

您想使用线性环绕。这里有一篇很棒的博客文章。这当然假设您的纹理瓷砖完美。您只需要设置线性包装模式,代码示例如下:

// 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();
于 2013-08-09T18:56:35.677 回答