XNA滚动背景

XNA滚动背景,xna,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 do

嗨,我正在做一个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:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
//精灵的大小
公共矩形尺寸;
//用于从原始图像向上或向下调整精灵的大小
公众浮标=1.0f;
//创建一个Texture2D实例,该实例将
//包含背景纹理。
纹理2D背景;
//创建一个矩形,该矩形将定义
//主游戏屏幕的限制。
矩形主机;
私有GamePadState GamePadState;
私有键盘状态键盘状态;
公共级摄像机
{
公共摄影机(视口)
{
原点=新矢量2(viewport.Width/2.0f,viewport.Height/2.0f);
缩放=1.0f;
}
公共向量2位置{get;set;}
公共向量2原点{get;set;}
公共浮动缩放{get;set;}
公共浮点旋转{get;set;}
公共矩阵GetViewMatrix(矢量2视差)
{
//要添加视差,只需将其乘以位置
返回矩阵.CreateTranslation(新矢量3(-位置*视差,0.0f))*
//下一行有一个陷阱。请参见下面的注释。
矩阵.CreateTranslation(新矢量3(-Origin,0.0f))*
矩阵.CreateRotationZ(旋转)*
矩阵.CreateScale(缩放,缩放,1)*
矩阵.CreateTranslation(新向量3(原点,0.0f));
}
}
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
IsMouseVisible=true;
graphics.IsFullScreen=true;
}
受保护的覆盖无效初始化()
{
gamePadState=GamePad.GetState(PlayerIndex.One);
keyboardState=Keyboard.GetState();
base.Initialize();
}
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//加载背景内容。
背景=Content.Load(“图像\\muur”);
//设置矩形参数。
mainFrame=新矩形(0,0,GraphicsDevice.Viewport.Width,GraphicsDevice.Viewport.Height);
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色。黑色);
//画背景。
//开始构建精灵。
spriteBatch.Begin();
//画背景。
绘制(背景、主机、颜色、白色);
//结束建造精灵。
spriteBatch.End();
基础。抽签(游戏时间);
}
}

如何实现此功能?

我看不出您在哪里使用摄影机变换

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

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