1

嗨,我正在开发 2D 游戏,并且正在研究滚动背景,但无论我尝试什么,它都不会滚动。

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    //The size of the Sprite
    public Rectangle Size;

    //Used to size the Sprite up or down from the original image
    public float Scale = 1.0f;

    // Create an instance of Texture2D that will
    // contain the background texture.
    Texture2D background;

    // Create a Rectangle that will definee
    // the limits for the main game screen.
    Rectangle mainFrame;
    private GamePadState gamePadState;
    private KeyboardState keyboardState;
    public class Camera
    {
        public Camera(Viewport viewport)
        {
            Origin = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);
            Zoom = 1.0f;
        }

        public Vector2 Position { get; set; }
        public Vector2 Origin { get; set; }
        public float Zoom { get; set; }
        public float Rotation { get; set; }

        public Matrix GetViewMatrix(Vector2 parallax)
        {
            // To add parallax, simply multiply it by the position
            return Matrix.CreateTranslation(new Vector3(-Position * parallax, 0.0f)) *
                // The next line has a catch. See note below.
                   Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
                   Matrix.CreateRotationZ(Rotation) *
                   Matrix.CreateScale(Zoom, Zoom, 1) *
                   Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
        }
    }

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
        graphics.IsFullScreen = true;
    }

    protected override void Initialize()
    {
        gamePadState = GamePad.GetState(PlayerIndex.One);
        keyboardState = Keyboard.GetState();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);         

        // Load the background content.
        background = Content.Load<Texture2D>("Images\\muur");

        // Set the rectangle parameters.
        mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);
        // Draw the background.

        // Start building the sprite.
        spriteBatch.Begin();

        // Draw the background.
        spriteBatch.Draw(background, mainFrame, Color.White);

        // End building the sprite.
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

我怎样才能实现这个功能?

4

1 回答 1

2

我看不到您在哪里使用相机变换...

你应该有一个视差矢量并更新它......

 Vector2 parallax_position;
 float parallax_speed;

 public void Update (Gametime time)
 {
       parallax_position += parallax_speed * Vector2.UnitX * (float) time.elapsed.totalseconds;
 }

然后在 Draw 方法中,你应该在你的 spritebatch 中使用它......

 public void Draw()
 {
      spriteBatch.begin(..,...,..,..,.., GetCameraTransform(Parallax));
      ...
 }
于 2012-10-23T07:42:53.150 回答