1
我很新的XNA框架現在我可以做繪畫,動畫等,但我遇到了一個問題相機顯示光標。XNA 4.0二維相機光標有效
我想要使用光標作爲主控制器(選擇,在地圖上移動等)來實現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));
}
}
}
}
而且還Cursor類
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);
我仍然無法完全理解的概念世界和視圖矩陣及其數學。