Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

I want something like this or this but I don't find anything about this in XNA.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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;
    }
share|improve this answer
    
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 –  bentham 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. –  Roy T. 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 –  bentham 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. –  Roy T. Apr 17 '12 at 18:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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