This site requires JavaScript, please enable it in your browser!
Greenfoot back
Gazzzah
Gazzzah wrote ...

2012/5/1

Mouse is down

Gazzzah Gazzzah

2012/5/1

#
I'm having trouble specifying an if statement for when the mouse is being held down. My attempts are as follows.
//Attempt 1
MouseInfo mi = Greenfoot.getMouseInfo();
            if (mi != null)
            {
                int btn = mi.getButton();
                if(Greenfoot.mousePressed(null) && btn == 1) leftClick = true;
                //else leftClick = false;
                if (Greenfoot.mousePressed(null) && btn == 3) rightClick = true;
                //else rightClick = false;
            }
            if (Greenfoot.mouseClicked(null))
            {
                leftClick = false;
                rightClick = false;
            }

//Attempt 2
            
            if(Greenfoot.mousePressed(null))
            {
                leftClick = true;
            }
            if (leftClick && Greenfoot.mouseClicked(null))
            {
                leftClick = false;
            }

//Attempt 3

            if(Greenfoot.mousePressed(null) && !Greenfoot.mouseClicked(null))
            {
                leftClick = true;
            }
            else
            {
                leftClick = false;
            }
Gazzzah Gazzzah

2012/5/1

#
I copy pasted this in a hurry, ignore the 'quote outs' for the 'else' statements.
danpost danpost

2012/5/1

#
The following code will capture all button presses and releases (clicks). Using the following variables
int buttonsDown = 0;
final int btnNEITHER = 0, btnLEFT = 1, btnRIGHT = 3;
with this code
if (Greenfoot.mousePressed(null))
{
    MouseInfo mi = Greenfoot.getMouseInfo();
    buttonsDown += mi.getButton();
    if (buttonsDown == btnLEFT) System.out.println("Left button pressed");
    if (buttonsDown == btnRIGHT) System.out.println("Right button pressed");
    if (buttonsDown == btnLEFT + btnRIGHT) System.out.println("Both buttons pressed");
}
if (buttonsDown != btnNEITHER && Greenfoot.mouseClicked(null)) 
{
    MouseInfo mi = Greenfoot.getMouseInfo();
    int wasDown = buttonsDown;
    buttonsDown -= mi.getButton();
    int buttonReleased = wasDown - buttonsDown;
    if (buttonReleased == btnLEFT) System.out.println("Left button released");
    if (buttonReleased == btnRIGHT) System.out.println("Right button released");
    if (buttonsDown == btnNEITHER) System.out.println("(all buttons released)");
}
Gazzzah Gazzzah

2012/5/1

#
Never mind, I found the problem: it was within another method meaning it only checked after a certain number of acts. So it was possible to skip releasing the mouse.
danpost danpost

2012/5/1

#
Just a note on it, anyway (maybe it would come in handy for someone). Line 9 does not need the 'buttonsDown != btnNEITHER' part of the condition as a click cannot occur unless one IS down (it does not hurt to leave it in there, either).
Gazzzah Gazzzah

2012/5/1

#
But that might be good to take a look at. Thanks Danpost
You need to login to post a reply.