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

2014/3/14

Mouse Buttons

1
2
3
4
Pointifix Pointifix

2014/3/14

#
Hi. I can get the Button that is clicked with mouse.getButton(), but how do i get it when the button goes up again, so it will only go into an if() if the button is released, you know what i mean?
Zamoht Zamoht

2014/3/14

#
Use Greenfoot.mouseClicked(Class class);
Pointifix Pointifix

2014/3/14

#
does not check if it was rightklick
Nitrouspiggy Nitrouspiggy

2014/3/14

#
have it call a method that waits for the button to be released:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void act()
{
    MouseInfo m = Greenfoot.getMouseInfo();
    if(m != null) checkClick(m);
}
 
private void checkClick(MouseInfo m)
{
    if(m.getButton() == 1) //left-click
    {
        while(m.getButton() != 0); //This is more than likely the worst way to wait, but it's what came into my head at the time :)
        //Your left-click code here
    }
    else if(m.getButton() == 3) //right-click
    {
        while(m.getButton() != 0);
        //Your right-click code here
    }
}
As mentioned, it's probably not the most efficient way of checking, but it should work. I shall test it now.
Nitrouspiggy Nitrouspiggy

2014/3/14

#
Ok, my code seems to crash my Greenfoot. I knew it would be bad XD
danpost danpost

2014/3/14

#
I have noticed that the mouse button detection provided is a bit sketchy. As long as one button is used at a time, there is no problem. But, when the buttons are pressed and released in a mixed fashion, sometimes a press or release is missed. This makes it impossible to get all mouse button releases. Also, I find that increasing the scenario speed helps to minimize the missing presses/clicks. For the right mouse button 'getMouseInfo().getButton()' should return 3.
Pointifix Pointifix

2014/3/14

#
jeah no bad attempt, but the while is bad, cause greenfoot will get stuck in the loop
danpost danpost

2014/3/14

#
I used the following for testing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import greenfoot.*;
 
public class Buttons extends World
{
    final int btnNONE = 0, btnLEFT = 1, btnRIGHT = 3;
 
    public Buttons()
    {
        super(800, 600, 1);
    }
 
    public void act()
    {
        MouseInfo mi = Greenfoot.getMouseInfo();
        if (Greenfoot.mousePressed(null))
        {
            int button = mi.getButton();
            if (button == btnLEFT) System.out.println("Left");
            if (button == btnRIGHT) System.out.println("Right");
        }
        if (Greenfoot.mouseClicked(null))
        {
            int button = mi.getButton();
            if (button == btnLEFT) System.out.println("No left");
            if (button == btnRIGHT) System.out.println("No right");
        }
    }
}
Create a new world and copy the code above into it. Run it at normal speed using one button at a time and then with mixing the buttons up. Then increase the speed of the scenario and re-evaluate.
Pointifix Pointifix

2014/3/14

#
jeah but every tick it returns that without releasing between
danpost danpost

2014/3/14

#
Pointifix wrote...
jeah but every tick it returns that without releasing between
That is very vague. I have no idea what you are trying to say! Please elaborate in detail.
Pointifix Pointifix

2014/3/14

#
it will join the if() every tick, but i just want it to join once per click
danpost danpost

2014/3/14

#
Please show the code you are using (I should not have to tell you that every time you have a problem).
Pointifix Pointifix

2014/3/14

#
1
2
3
4
5
if(mouse != null && mouse.getButton() == 3 && !Greenfoot.mouseDragEnded(this))
        {
            item_rotation++;
            if(item_rotation == 4)item_rotation = 0;
        }
danpost danpost

2014/3/14

#
You are missing the 'Greenfoot.mouseClicked(this)' condition.
danpost danpost

2014/3/14

#
Pointifix wrote...
1
2
3
4
5
if(mouse != null && mouse.getButton() == 3 && !Greenfoot.mouseDragEnded(this))
        {
            item_rotation++;
            if(item_rotation == 4)item_rotation = 0;
        }
Actually, you do not need to get the MouseInfo object before the 'if' statement. Remove it and use this:
1
if (Greenfoot.mouseClicked(this) && !Greenfoot.mouseDragEnded(this) && Greenfoot.getMouseInfo().getButton() == 3)
There are more replies on the next page.
1
2
3
4