# How do I make an area in the screen clickable with XNA?

I am programming a penalty shoout game, my question is how make an area in the screen clickable with XNA? I know that for seeing the mouse I have to use:

this.IsMouseVisible = true;


But I want this for the direction of the soccer ball? Any ideas?

You will have to do a couple of things, you have to detect if the mouse button was clicked. You do this by checking if it wasn't pressed the previous frame and is pressed currently.

Then you have to get the mouse position and check if that position was inside the clickable area. You can use a simple rectangle as the clickable area.

Anyway it would in XNA lead to code something like this:

MouseState prevMouseState;
MouseState mouseState = Mouse.GetState();
Rectangle clickableArea = new Rectangle(10, 10, 500, 500);

public void DetectClick()
{
if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released)
{
// We now know the left mouse button is down and it wasn't down last frame
// so we've detected a click
// Now find the position
Point mousePos = new Point(mouseState.X, mouseState.Y);
if (clickableArea.Contains(mousePos))
{
// Yay there was a click inside the clickable area
}
}

// Store the mouse state so that we can compare it next frame
// with the then current mouse state
prevMouseState == mouseState;
}

• I need to change this line prevMouseState = mouseState, but I still have a question, I call the method in the Update method, but nothings happens I still trying to figure out, thanks for answering Apr 15 '12 at 17:06
• Oh yes, that was supposed to be '=' not '==' indeed. You have to put your own code that determines what happens on a click inside the inner if. Apr 15 '12 at 21:10
• just another question please I am trying to make a penalty shootout game I make the goalkeeper a rectangle for the collision of the ball, if the ball intersect the goalkeeper its not a goal, but if the ball was inside the area of the line Its a goal, how did you do this kind of game?? I am on the right way Apr 17 '12 at 17:03
• First you check if the ball is colliding with the the rectangle that represents the goal. If that is true you do a second check to see if it's not colliding with the rectangle of the goal keeper. So you just add if(!goalkeeperArea.Contains(mousePos)){ // yay goal! } inside the inner if. Apr 17 '12 at 18:00