XNA DrawableGameComponent鼠标输入问题
我正在开发一个简单的棋盘游戏,其中我的棋盘被表示为一个DrawableGame组件。在线路板的更新方法中,我正在检查鼠标输入,以确定线路板上的哪个字段被单击。我遇到的问题是,每5-6次鼠标点击只能注册一次鼠标点击 鼠标点击代码是基本的:XNA DrawableGameComponent鼠标输入问题,xna,mouse,drawablegamecomponent,Xna,Mouse,Drawablegamecomponent,我正在开发一个简单的棋盘游戏,其中我的棋盘被表示为一个DrawableGame组件。在线路板的更新方法中,我正在检查鼠标输入,以确定线路板上的哪个字段被单击。我遇到的问题是,每5-6次鼠标点击只能注册一次鼠标点击 鼠标点击代码是基本的: public override void Update(GameTime gameTime) { MouseState mouseState = Mouse.GetState(); Point mouseCell = ne
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中,我只是将我的板添加到组件集合中
有人知道我在这里遗漏了什么吗
编辑:事实上,不仅仅是鼠标,键盘输入也不能正常工作,所以我可能把DrawableGameComponent的实现搞砸了,尽管我不知道怎么做
EDIT2:在这里发现了一些人:在这里:有着非常相似的问题。
调试失败后,我放弃了DrawableGameComponent,实现了手动LoadContent/Update/Draw调用,并将所有输入收集放在game.cs中。工作起来很有魅力。
但是,如果有人对可能出现的错误有任何解释(看起来DrawableGameComponent以某种方式阻塞了输入),我真的很想知道。问题是,如果鼠标在
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);
}
在您的解决方案中进行搜索,并尝试查找可能使用鼠标/键盘GetState的其他位置。我不确定XNA是否适用,但您可能在其他地方执行GetEvent,从而重置键盘/鼠标状态。您是否注册了许多GameComponent实例?您的游戏中还更新了哪些内容?@Marking:这当然可能发生,但GetState只被调用了一次。@liortal:即使只有一个GameComponent实例,问题仍然存在,上面所述的鼠标代码是唯一的更新代码。您如何知道鼠标单击只会每5-6次注册一次?点击是否被点击的参考是什么?在您的鼠标点击检测器中(如果您在这里发布的话,则在IF的主体中),添加一行来调用Debug.WriteLine(“鼠标点击”);然后开始游戏,点击鼠标左键,在VisualStudio的输出视图中查看此文本是否在每次单击或每5-6次单击时写入。