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 developing a card game in XNA.

Is there any OnClick event in XNA for objects?

I am trying to make cards move when the player clicks on them. In this project, there is a Sprite class that draws the card, but I am a little stuck because I don't know how to use OnClick events or anything like that.

share|improve this question
add comment

6 Answers

You'll need to implement it yourself. Try looking at the tutorial at: http://bluwiki.com/go/XNA_Tutorials/Mouse_Input

share|improve this answer
add comment

When working with XNA in general you have to move from an event driven code paradigm to a loop driven code paradigm. Your update code loops 60 times/sec. So each time, look at the state of the mouse and if a button is down & pointer is within your rect, then branch to code you would normally place in an OnClick event.

share|improve this answer
2  
It seems like that may be semantically correct, but also wrong in my opinion. There are no events when you get down to looking at a thread and what the current state during each loop is. This is why we create our own events and write their corresponding handlers. I'de say using flow control to branch to every instance of mouse down would become crazy in a big project. Instead, register an event and write handlers wherever needed. Events were thought up in the first place for this reason. –  brandon May 1 '11 at 16:25
4  
Brandon: You don't "use flow control to branch to every instance of mouse down". You record the current and previous mouse state once at the beginning of each frame, and then in every place that your code responds to a mousedown press you simply do if(MouseLeftPress()){ DoSomething(); }, where MouseLeftPress() is a method you write to compare the current and previous left button mouse states. I find this easier than implementing events most of the time. –  Olhovsky May 2 '11 at 2:14
    
Simple polling gets really ugly beyond your basic arcade controls. I have an input manager class that captures the gamepad state as FlagAttributes and allows you to map delegates to different combinations of buttons. Next step up is to implement timing. All this debate just reinforces Steve H's point though, you're giving a polling system, and you are free to build upon it to support whatever paradigm you prefer. –  michael.bartnett May 2 '11 at 13:01
add comment

XNA only gives you just enough to build your own input classes - and you should do so considering the utility they offer.

I would recommend the mouse input class described in this blog post (source code). It detects a button being pressed, held or released, and lets you get that information either via polling or events.

On a related note, the same blog post also provides keyboard and gamepad input device classes.

share|improve this answer
add comment

If your game is 3D you can implement picking, which is described here: http://create.msdn.com/en-US/education/catalog/sample/picking_triangle. Basically this creates a ray from the camera to where the mouse clicks (with a little matrix unprojecting in there), then you check to see if any of your objects were intersected by the ray.

If your game is 2D, you should be able to translate window coordinates to game coordinates fairly easily. Then check to see if the selected coordinates are within the bounds of any of your objects.

share|improve this answer
add comment

The actual code for checking a mouse click in XNA is somthing like this;

    MouseState previousMouseState;
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        //store the current state of the mouse
        previousMouseState = Mouse.GetState();
    }


    protected override void Update(GameTime gameTime)
    {
        // .. other update code


        //is there a mouse click?
        //A mouse click occurs if the goes from a released state
        //in the previous frame  to a pressed state 
        //in the current frame
        if (previousMouseState.LeftButton == ButtonState.Released 
            && Mouse.GetState().LeftButton == ButtonState.Pressed)
        {
            //do your mouse click response...

        }

        //save the current mouse state for the next frame
        // the current 
        previousMouseState = Mouse.GetState();

        base.Update(gameTime);
    }
share|improve this answer
add comment

The simplest way to see if the mouse is clicked is this

//Create this variable
MouseState mouseState;

now in the update method add this

mouseState = Mouse.GetState();

if (mouse.RightButton == ButtonState.Pressed)
{
     //Do Stuff
}

hope this helped

share|improve this answer
add comment

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.