3

ゲームボードが DrawableGameComponent として表される単純なボードゲームを開発しています。ボードの更新メソッドでは、マウス入力をチェックして、ボード上のどのフィールドがクリックされたかを判断しています。私が遭遇している問題は、マウス クリックが 5 ~ 6 回のクリックごとに 1 回しか登録されないことです。

マウス クリック コードは基本的なものです。

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 が何らかの形で入力を詰まらせているように見えます)、私は本当に知りたいです。

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 に答える