XNA滚动背景

时间:2012-10-23 06:40:49

标签: xna

嗨,我正在制作一个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);
    }
}

如何实现此功能?

1 个答案:

答案 0 :(得分: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));
      ...
 }