xnaFan's Blog » MouseState http://xnafan.net XNA tutorials, code and games Thu, 13 Feb 2014 15:17:45 +0000 en-US hourly 1 https://wordpress.org/?v=4.2.22 MouseState ExtensionMethod http://xnafan.net/2010/01/mousestate-extensionmethod/ http://xnafan.net/2010/01/mousestate-extensionmethod/#comments Wed, 20 Jan 2010 07:24:00 +0000 http://xnafan.net/?p=83 Just a little helpermethod to get the mouse's position as a Vector2.

For those of you who still haven't gotten started with extension methods here's a quick writeup'n'sample

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XNAFAN
{
public static class MouseStateExtensionMethods
{

///

/// Returns the mouseposition as a Vector2
///

/// The current MouseState /// The mouseposition as a Vector2
public static Vector2 GetPosition(this MouseState mouse)
{
return new Vector2(mouse.X, mouse.Y);
}
}
}

This way you don't have to convert x and y every time along the lines of
MouseState mouseState = Mouse.GetState();
Vector2 position = new Vector2(mouseState.X, mouseState.Y);

Instead you just add a reference to the code with the extensionmethod and this enables you to write:

Vector2 position = mouseState.GetPosition();

Nifty - eh...? :-)

]]>
http://xnafan.net/2010/01/mousestate-extensionmethod/feed/ 0