Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Check if mousebutton was clicked (pressed, then released)  (Read 3472 times)

0 Members and 1 Guest are viewing this topic.

Enok

  • Newbie
  • *
  • Posts: 16
    • View Profile
Check if mousebutton was clicked (pressed, then released)
« on: November 17, 2014, 12:33:49 pm »
Hello.

I'm having a bit of a problem here with a Method. See, I need this method to return true only when the mousebutton was pressed and then released.

Code: [Select]

        private static bool MouseClicked(Mouse.Button Button)
        {
            if (Mouse.IsButtonPressed(Button))
            {
                if (!Mouse.IsButtonPressed(Button))
                    return true;
                else
                    return false;
            }
            else
                return false;
        }


I know why the above code fails to do what I want, it's quite simple, it's running through a loop and so, the mouse-clicking has to be pretty damn precise(read lucky) in order for the method to return true..

Now, I'm at a total loss here tbh, I'm not quite sure how this could be solved. Any ideas? 

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: Check if mousebutton was clicked (pressed, then released)
« Reply #1 on: November 17, 2014, 12:40:18 pm »
Use RenderWindow's MouseButtonReleased event.

Enok

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Check if mousebutton was clicked (pressed, then released)
« Reply #2 on: November 17, 2014, 12:43:48 pm »
I took 2 sips of my coffee and now I function properly again. I always do this, lel. I ask for a solution, then I solve it 2 minutes later, hehe.

Code: [Select]
        private static bool curState;
        private static bool MouseClicked(Mouse.Button Button)
        {
            if (Mouse.IsButtonPressed(Button))
                curState = true;

            if (curState)
            {
                if (!Mouse.IsButtonPressed(Button))
                {
                    curState = false;
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }