这一直困扰着我 - 当我使用下面的代码来增加每次鼠标点击的选择时:
if (m.LeftButton == ButtonState.Pressed)
currentSelection++;
然后currentSelection增加了一吨,因为这个代码在我的Update()函数中并且按照设计运行每一帧因此增加了currentSelection .您几乎没有机会快速单击和释放以防止currentSelection增加多个.
现在我的问题是我应该做什么,所以每当我点击鼠标一次,它只会增加currentSelection一次,直到我再次点击下来.
您需要比较当前鼠标状态和上次更新的鼠标状态.
在你的班级你将MouseState mouseStateCurrent, mouseStatePrevious;宣布,所以它将是这样的:
mouseStateCurrent = Mouse.GetState();
if (mouseStateCurrent.LeftButton == ButtonState.Pressed &&
mouseStatePrevious.LeftButton == ButtonState.Released)
{
currentSelection++;
}
mouseStatePrevious = mouseStateCurrent;
Run Code Online (Sandbox Code Playgroud)
所以它会检测到你之前按下它的时间,然后你释放它 - 只有它被认为是一个点击它将添加currentSelection.