3

我正在开发一个简单的棋盘游戏,其中我的游戏板表示为 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 中,我只是将我的板添加到组件集合中。

有谁知道我在这里可能会错过什么?

编辑:实际上,不仅仅是鼠标,键盘输入也不能正常工作,所以我可能搞砸了 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 以某种方式阻塞输入),我真的很想知道。

4

1 回答 1

0

问题是,如果鼠标在MouseState mouseState = Mouse.GetState();和之间改变状态previousMouseState = mouseState;,那么previousMouseStatemouseState将具有相同的值。

通过将它们移动到尽可能靠近彼此的线,这几乎是不可能的。

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);
}
于 2011-07-26T09:34:25.170 回答