XNA 鼠标移动
我之前在 XNA 中制作过一些游戏,现在我即将开始一个新项目。我想做的一件事是让鼠标移动。
只是为了澄清(因为我已经看到一些类似的问题导致混乱)我想获得鼠标的移动。不是光标的位置或从一帧到下一帧的位置变化。我只是想要有关鼠标如何移动的数据。
在我之前的游戏中,我只是将(隐藏的)光标重置到视口的中间,然后查看位置的变化。然而,这似乎有点作弊,并导致我的代码中出现一些看起来混乱的计算。
那么有没有办法让鼠标的移动返回到程序中呢?
谢谢, 马特
编辑:
回应第一条评论。在这种情况下,我指的光标位置是鼠标指针在屏幕上的位置。我所说的鼠标移动是物理上移动鼠标。
XNA 中的“鼠标”一词似乎与指针(或光标)同义。
我遇到的问题是,尽管鼠标向左移动,但我无法在程序中获得该移动,因为光标位于屏幕边缘。
I've made a few games in XNA before and I'm about to start a new project. One thing I'd like to do is have the mouse movement.
Just to clarify (as I've seen some similar questions lead to confusion) I want to get the movement of the mouse. Not the position of the cursor or the change in position from one frame to the next. I would simply like data about how the mouse has been moved.
In my previous game I simply reset the (hidden) cursor to the middle of the viewport and looked at the change in position. However this seems at little bit of a cheat and leads to some messy looking calculations in my code.
So is there any way to have the mouse movement returned to the program?
Thanks,
Matt
Edit:
In response to first comment. The position of the cursor I'm refering to in this case is the position onscreen of the mouse pointer. The movement of the mouse I'm refering to is pysically moving the mouse.
XNA seems to have the word mouse to be synonymous with pointer (or cursor).
The problems I'm having is that despite the mouse moving left, I can't get that movement in the program as the cursor is at the edge of the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MouseState 类仅包括鼠标光标的位置等。正如您所提到的,这个位置将夹到屏幕的边缘。除了通过 MouseState 类之外,无法确定用户是否移动了鼠标。特别是,没有指示鼠标移动方向的 MouseDirection、MouseTangent 或 MouseAngle 属性。
作为解决方法,您可以调用 SetPosition 强制光标位于屏幕中间,这样你就可以随时知道鼠标移动的方向。事实上,SetPosition 甚至推荐这种方法:
The MouseState class includes only the position of the mouse cursor, among other things. This position will, as you mentioned, clip to the edges of the screen. There is no way to determine whether the user moved the mouse other than through the MouseState class. In particular, there is no MouseDirection, MouseTangent, or MouseAngle property that indicates which direction the mouse was moved.
As a workaround, you can call SetPosition to force the cursor at the middle of the screen, so you can always know which direction the mouse has moved. In fact, SetPosition even recommends this approach:
我对XNA的了解非常有限,因为游戏开发不是我的开发主线。
话虽这么说,根据我对 XNA 行为的理解,您确实需要计算帧之间的位置变化,以了解鼠标移动了多少。
通过 XY 坐标的变化,您可以根据这个简单的测量获得所有类型的数据。
My understanding of XNA is very limited, as game development isn't my main line of development.
That being said, for what I understand of the bejavior of XNA, you really need to calculate the variation of position between frames to see how much the mouse has moved.
With the variation in the X-Y coordinate, you can get all kind of data based on this simple measure.
如果您想要鼠标的实际移动,您只需创建一个在给定事件上重置的鼠标上次移动的数组或列表,例如。
经过时间> 1f
或IsMouseReleased
。它看起来像这样:
If you want the acctual movement of the mouse you can just create a array or list of the mouse last movements that resets on a given event, ex.
elapsedtime > 1f
orIsMouseReleased
.it would look something like this: