简体   繁体   English

XNA视差背景不会显示所有图层

[英]XNA Parallaxing background wont show all the layers

Im playing around with the Platformer Starter Kit and so far I've added in horizontal and vertical "camera" movement and Im trying to add inn a parallaxing background. 我正在使用Platformer Starter Kit,到目前为止,我已经添加了水平和垂直“相机”运动,并且我试图添加客栈的视差背景。 The problem is that after two background layers it stops showing the rest of them. 问题在于,在两个背景层之后,它将停止显示其余部分。 Im very new to XNA and need a little help :). 我是XNA的新手,需要一点帮助:)。 Heres a pic of the problem: 这是问题的照片:

层失败

Heres the code. 这是代码。 Please tell me if you need some more :) 请告诉我您是否还需要其他:)

Layer classes: 图层类:

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


}

Heres the draw method in my Level.cs with my ScrollCamera (dont know if ScrollCamera has anything to do with it) 使用我的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);

    }

And I think thats it. 我认为就是这样。 Please tell me if you need some more code :) 请告诉我是否需要更多代码:)

You want to use Linear Wrapping. 您要使用线性包装。 There's an excellent blog post on it right here. 这里有一篇很棒的博客文章。 This assumes of course that your texture tiles perfect. 当然,这假定您的纹理平铺完美。 You just simply need to to set your linear wrapping mode, code example below: 您只需要设置线性包装模式,下面的代码示例:

// 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();

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

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