【问题标题】:XNA 4.0 2D Camera With Cursor ActiveXNA 4.0 2D 相机,光标处于活动状态
【发布时间】:2014-03-28 18:44:45
【问题描述】:

我是 XNA 框架的新手 现在我可以做绘图、动画等,但是我遇到了 Camera 显示 cursor 的问题。

我想实现一个 2D 游戏,其中您使用光标作为主控制器(选择、在地图上移动等)。我希望通过光标右键单击并拖动来控制相机。

我为移动它创建了非常简单的逻辑,但它不起作用。 每当我按下 RButton 并拖动它时,相机都可以正常工作。

再次执行时会弹出问题。整体视图重置为初始位置。我想问题在于鼠标相对于世界的位置。

我正在添加我的相机类

class TCCamera : IDrawable
{
    public Matrix transformation;
    Viewport viewport;
    Vector2 centre;

    public TCCamera(Viewport vport)
    {
        viewport = vport;
        this.centre = new Vector2();
    }
    public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager Content)
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        throw new NotImplementedException();
    }

    public Matrix Translate
    {
        get
        {
            return this.transformation;
        }
    }

    public void Update(GameTime gameTime)
    {
        MouseState mstat = Mouse.GetState();



        if (mstat.RightButton == ButtonState.Pressed)
        {
            this.centre.X = mstat.X;
            this.centre.Y = mstat.Y;
            transformation =  Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0));
        }

    }


}
}

还有游标类

class TCCursor : IDrawable
{
    Texture2D tex;
    public Vector2 pos;
    Rectangle bBox;

    public TCCursor()
    {
        this.pos = new Vector2();
        this.bBox = new Rectangle(0, 0, 50, 50);
    }

    public void LoadContent(ContentManager Content)
    {
        tex = Content.Load<Texture2D>("cursor");
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(tex, bBox, Color.White);
    }
    public void Update(GameTime gameTime)
    {
        MouseState value = Mouse.GetState();
        this.pos.X = value.X;
        this.pos.Y = value.Y;
        this.bBox.X = value.X;
        this.bBox.Y = value.Y;
    }

}

我也是这样画的

spriteBatch.Begin(SpriteSortMode.Immediate, 
            BlendState.AlphaBlend,
            null, null, null, null, camera.Translate);

我仍然不能完全理解世界和视图矩阵及其数学的概念。

【问题讨论】:

    标签: c# xna camera cursor 2d


    【解决方案1】:

    一段时间后,我找到了使用逆矩阵的解决方案。 我发现这非常有用:Tutorial XNA Camera

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多