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 th开发者_JS百科e screen.
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.
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.
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);
}
精彩评论