Advertisement

MouseState check if clicked

Started by January 07, 2015 01:20 AM
5 comments, last by tmer1 10 years ago

In chapter 6 of my C# book we learn how to program an If check for a mouse click.

It says that to do this we need:

1. The player did not press the mouse button during the last Update method;

2. In the current Update method, the player presses the mouse button.

But this says nothing about releasing the mouse button ?? I ran the program and it only takes into account pressing, not releasing...

Whats going on?


protected override void Update(GameTime gameTime)
    {
        previousMouseState = currentMouseState;
        currentMouseState = Mouse.GetState();

        if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
            calculateAngle = !calculateAngle;

        if (calculateAngle)
        {
            double opposite = currentMouseState.Y - barrelPosition.Y;
            double adjacent = currentMouseState.X - barrelPosition.X;
            angle = (float)Math.Atan2(opposite, adjacent);
        }
        else
            angle = 0.0f;
    }

"Released" in this case means "not currently pressed". It's checking if the button was not pressed in the previous frame.

Think of the buttons like "states" rather than "events". "Pressed" means "the button is held down". "Released" is the opposite.

Advertisement
I believe "Pressed" means "the button is/was down" and "Released" means "the button is/was up".

So the code really means:

"if it is currently down this frame and it was previously up on the last frame"


The goal of code like that is to prevent the condition check from being true *every* frame that the button is down.


While the game is running, the state of the mouse button will look like this each frame:


Released
Released
Released
Released
Released
Released
Released
Released
Pressed    <- this is the only frame that you want to treat the action as a "click"
Pressed
Pressed
Pressed
Pressed
Released
Released
Released
Released
Pressed     <- another click

Before you read the next couple of lines, think about these questions: in this program, what do think should happen when the mouse button is released? How would the code you've written reflect a release rather than a press?

The code segment you've posted is designed to cause a response when the mouse button is clicked, which is to say that it was not pressed (that's previousMouseState.LeftButton == ButtonState.Released) and now it is (currentMouseState.LeftButton == ButtonState.Pressed). It's not designed to monitor an ongoing button press, nor does it do anything when the button is released.

EDIT: Damn, I was sure my post was quick enough...

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

ok thanks but the book specifically said:

"...If the player has clicked (meaning pressing and then releasing the mouse button)"

the previous code has nothing to do with releasing afterwards it just checks for state released, then next state pressed. This is not the usual definition of click is it?

ok thanks but the book specifically said:

"...If the player has clicked (meaning pressing and then releasing the mouse button)"

the previous code has nothing to do with releasing afterwards it just checks for state released, then next state pressed. This is not the usual definition of click is it?


The book might just have a typo smile.png

If you want to do that (treating the frame on which the mouse goes from down to up), then you can just swap the pressed and released parts of the code:


if (currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
Advertisement

ah ok that solves it. thanks.

This topic is closed to new replies.

Advertisement