gpt4 book ai didi

XNA DrawableGameComponent鼠标输入问题

转载 作者:行者123 更新时间:2023-12-04 18:22:40 25 4
gpt4 key购买 nike

我正在开发一个简单的棋盘游戏,我的游戏棋盘表示为一个 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 以某种方式阻塞了输入)我真的很想知道。

最佳答案

问题是,如果鼠标在 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);
}

关于XNA DrawableGameComponent鼠标输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6806082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com