在 XNA 中每次单击鼠标/按键增加一次数量

发布于 2024-08-24 16:49:19 字数 308 浏览 3 评论 0原文

这最近一直困扰着我——当我使用如下代码来增加每次鼠标点击的选择时: if (m.LeftButton == ButtonState.Pressed) 当前选择++; 然后 currentSelection 增加很多,只是因为这段代码位于我的 Update() 函数中,并且按照设计,运行每一帧,因此增加了 currentSelection。您几乎不可能以足够快的速度单击并释放以防止 currentSelection 增加多个。

现在我的问题是我应该做什么才能使它每次单击鼠标一次时,它只会增加一次 currentSelection 直到下次我再次单击鼠标时。

This has been bothering me lately- when I use some code like below to increase selection every mouse click:
if (m.LeftButton == ButtonState.Pressed)
currentSelection++;

Then currentSelection increases by a ton, simply because this code is in my Update() function and by design, runs every frame and so increases currentSelection. There is almost no chance you can click and release fast enough to prevent currentSelection from increasing more than one.

Now my question is what I should do to make it so everytime I click the mouse down once, it only increases currentSelection once until the next time I click down again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-08-31 16:49:19

您需要比较当前鼠标状态和上次更新的鼠标状态。

在你的类中,你将声明 MouseState mouseStateCurrent, mouseStatePrevious; ,所以它会是这样的:

mouseStateCurrent = Mouse.GetState();

if (mouseStateCurrent.LeftButton == ButtonState.Pressed &&
    mouseStatePrevious.LeftButton == ButtonState.Released)
{
    currentSelection++;
}

mouseStatePrevious = mouseStateCurrent;

所以它会检测到你上次按下它,然后释放它 - 只有那时它才被考虑作为点击,它将添加到 currentSelection

You will need to compare the current mouse state and the last update's mouse state.

In your class you'll have MouseState mouseStateCurrent, mouseStatePrevious; declared and so it'll be something like:

mouseStateCurrent = Mouse.GetState();

if (mouseStateCurrent.LeftButton == ButtonState.Pressed &&
    mouseStatePrevious.LeftButton == ButtonState.Released)
{
    currentSelection++;
}

mouseStatePrevious = mouseStateCurrent;

So it will detect that previous time you pressed it, then you release it - only then it is considered as a click and it will add to currentSelection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文