XNA Mouse Movement

后端 未结 3 534
孤街浪徒
孤街浪徒 2021-01-17 01:56

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

相关标签:
3条回答
  • 2021-01-17 02:11

    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.

    0 讨论(0)
  • 2021-01-17 02:12

    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);
    }
    
    0 讨论(0)
  • 2021-01-17 02:23

    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.

    0 讨论(0)
提交回复
热议问题