XNA 鼠标移动

发布于 2024-11-28 04:52:46 字数 419 浏览 1 评论 0原文

我之前在 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 技术交流群。

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

发布评论

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

评论(3

山田美奈子 2024-12-05 04:52:46

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:

When using this method to take relative input, such as in a
first-person game, set the position of the mouse to the center of your
game window each frame. This will allow you to read mouse movement on
both axes with the greatest amount of granularity.

ま昔日黯然 2024-12-05 04:52:46

我对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.

笑红尘 2024-12-05 04:52:46

如果您想要鼠标的实际移动,您只需创建一个在给定事件上重置的鼠标上次移动的数组或列表,例如。 经过时间> 1fIsMouseReleased
它看起来像这样:

List<Vector2> Movement = new List<Vector2>();    

public override void Update(GameTime gameTime)
{
    MouseState pms;
    MouseState ms = Mouse.GetState();

    pms = ms;

    Movement.Add(pms.X - ms.X, pms.Y - ms.Y);

    if(ms.LeftButton == ButtonState.Released){ Movement.Clear(); }

    base.Update(gameTime);
}

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 or IsMouseReleased.
it would look something like this:

List<Vector2> Movement = new List<Vector2>();    

public override void Update(GameTime gameTime)
{
    MouseState pms;
    MouseState ms = Mouse.GetState();

    pms = ms;

    Movement.Add(pms.X - ms.X, pms.Y - ms.Y);

    if(ms.LeftButton == ButtonState.Released){ Movement.Clear(); }

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