2012-10-23 139 views
1

嗨,我正在做一個2D遊戲,我正在研究滾動背景,但無論我嘗試它,都不會讓它滾動。XNA滾動背景

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

如何實現此功能?

回答

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