我正在开发一个简单的棋盘游戏,我的游戏板被表示为DrawableGameComponent。在板的更新方法中,我正在检查鼠标输入以确定单击板上的哪个字段。我遇到的问题是鼠标点击只能每5-6次点击一次注册。
鼠标点击代码是基本的:
public override void Update(GameTime gameTime)
{
MouseState mouseState = Mouse.GetState();
Point mouseCell = new Point(-1, -1);
if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
{
mouseCell = new Point(mouseState.X, mouseState.Y);
}
// cell calc ...
previousMouseState = mouseState;
base.Update(gameTime);
}
在Game.cs中,我只是将我的电路板添加到Components集合中。
有谁知道我在这里可能会缺少什么?
编辑:实际上,它不只是鼠标,键盘输入也无法正常工作,所以我可能搞砸了DrawableGameComponent实现,但我不知道怎么做。EDIT2:在这里找到一些人:http://forums.create.msdn.com/forums/p/23752/128804.aspx和这里:http://forums.create.msdn.com/forums/t/71524.aspx有非常相似的问题。 调试失败后,我抛弃了DrawableGameComponent并实现了手动LoadContent / Update / Draw调用,并将所有输入收集到了game.cs.奇迹般有效。 但是,如果有人有任何解释可能出错(看起来像DrawableGameComponent以某种方式克服了输入),我真的很想知道。
答案 0 :(得分:0)
问题是,如果鼠标在MouseState mouseState = Mouse.GetState();
和previousMouseState = mouseState;
之间更改状态,则previousMouseState
和mouseState
将具有相同的值。
通过将它们移动到尽可能彼此靠近的线条,这几乎是不可能的。
public override void Update(GameTime gameTime)
{
Point mouseCell = new Point(-1, -1); //moved this up, so that the mouseState and previousMouseState are as close to each other as possible.
MouseState mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
{
mouseCell = new Point(mouseState.X, mouseState.Y);
}
//It is almost imposible, that the mouse has changed state before this point
previousMouseState = mouseState; //The earliest place you can possibly place this line...
// cell calc ...
base.Update(gameTime);
}