I am creating a simple XNA game. My mainMenu class contains a list of strings for menu options wich I draw on the scren using spriteBatch.DrawString. I've set Vector2 variables to controll the position of the spritefont. Then I created 2 Rectangles (and took the values of the Vector2s) and 2 Colors that will be given a value if the mouse is/isn't currently hovering the drawn string. My mainMenu.cs class contains these variables:

Vector2 playGameButtonPos = new Vector2(303, 300);
Vector2 exitGameButtonPos = new Vector2(303, 350);
List<string> m_ButtonList = new List<string>();
Rectangle m_ButtonArea0 = new Rectangle(303, 300, 50, 30);
Rectangle m_ButtonArea1 = new Rectangle(303, 350, 50,30);
Color optionsColor0;
Color optionsColor1;

mainMenu.LoadContent(ContentManager Content):

Game1.g_MainMenuFont = Content.Load<SpriteFont>(".\\mainMenu\\mainMenuFontTex");

mainMenu.cs's constructor:

m_ButtonList.Add("Play Game");
m_ButtonList.Add("Exit Game");

mainMenu.Draw(SpriteBatch spriteBatch):

var m_MousePosition = new Point(Game1.g_MouseState.X, Game1.g_MouseState.Y);
if (m_ButtonArea0.Contains(m_MousePosition))
    optionsColor0 = Color.Yellow;
    optionsColor0 = Color.CornflowerBlue;
if (m_ButtonArea1.Contains(m_MousePosition))
    optionsColor1 = Color.Yellow;
    optionsColor1 = Color.Red;
spriteBatch.DrawString(Game1.g_MainMenuFont, m_ButtonList[0], playGameButtonPos, optionsColor0);
spriteBatch.DrawString(Game1.g_MainMenuFont, m_ButtonList[1], exitGameButtonPos, optionsColor1);


public static MouseState g_MouseState = Mouse.GetState();
public static SpriteFont g_MainMenuFont;
mainMenu m_MainMenu;

Game1's Initialize() method:

m_MainMenu = new mainMenu();

Game1's LoadContent() and Draw() methods:



But when I hover with my mouse on the position where the Rectangle should be, nothing happens.

  • \$\begingroup\$ You mean it returns false, right? Also, either your rectangle is out of place, in which case draw it so you know where it is, or your Game1.g_MouseState is out of sync. Try it directly with the Mouse class. \$\endgroup\$ – Ben Sep 16 '14 at 12:41
  • \$\begingroup\$ You are right I cannot see it drawn. But why my game is 800x600... And the positions are 303, 300 and 303, 350. \$\endgroup\$ – PowerUser Sep 16 '14 at 12:51

You're only asking for the mouse state once. You'll get the mouse state as it was at the time of initialisation. Specifically I think you'll get a copy of the mouse state at the time of initialisation, so it will appear to never change.

Call Mouse.GetState() directly in your Draw function instead.

  • 1
    \$\begingroup\$ I'd suggest you get Mouse.GetState() in your update function, and not in your draw function. Try to minimize the amount of processing to do in your draw calls as much as possible. \$\endgroup\$ – jgallant Sep 16 '14 at 12:44
  • \$\begingroup\$ Yes, if an Update function had been listed I would have suggested to put it in there instead. Really though the g_MouseState variable is entirely superfluous. \$\endgroup\$ – MrCranky Sep 16 '14 at 12:52
  • \$\begingroup\$ OK, I changed that. But the problem still occurs. I think it is an issue with my rectangle? \$\endgroup\$ – PowerUser Sep 16 '14 at 12:53
  • \$\begingroup\$ Actually I forgot to move MouseState g_MouseState = Mouse.GetState(); to Update(), my bad. \$\endgroup\$ – PowerUser Sep 16 '14 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.