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

2012/9/21

need help with mouseInfo

Gevater_Tod4711 Gevater_Tod4711

2012/9/21

#
I'm programming a shooter and want to have a machine gun where I don't have to klick the mouseButton every time I want to shoot. Is there a method in Greenfoot which returns true every time the left mouse key is down (like Greenfoot.isKeyDown()) ?
SPower SPower

2012/9/21

#
Maybe Greenfoot.mousePressed does this. You can also use the getKey method of an MouseInfo object.
Builderboy2005 Builderboy2005

2012/9/21

#
Greenfoot.mousePressed() returns true if the mouse button has been pressed down, and Greenfoot.mouseClicked() returns true when the button has been released. Therefore you can keep track yourself with a boolean variable whether or not the mouse button is currently down by observing those two cases. If the mouse button is ever pressed, set the variable to true, and if the mouse button is ever clicked, set the variable to false!
Gevater_Tod4711 Gevater_Tod4711

2012/9/22

#
Thank you that realy helped. @SPower there is a getKey method in MouseInfo? I've never seen it in the API. And the method in this class seems to be buggy. But now it works so it doesn't metter. Thank you
SPower SPower

2012/9/22

#
@Gevater Indeed, that method is buggy: I experienced it myself. @Builderboy Thanks, you also helped me!
davmac davmac

2012/9/22

#
What's this bug you're talking about?
Gevater_Tod4711 Gevater_Tod4711

2012/9/23

#
Sometimes when I use the method getButton in mouseInfo it sometimes returns the right value, sometimes the wrong one and sometimes not any value. I tried to use this code for the machine gun I was talking about above:
public void act() {
    ...
    mouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse.getButton() != 0) {
        //the gun should shoot but it does only sometimes.
    }
    ...
}
I also tried with mouse.betButton() == 1 which also didn't work. So I think this method seems to be buggy.
davmac davmac

2012/9/23

#
Well, in a quick experiment, it returns 0 if the mouse hasn't moved. I guess that is indeed a bug! However, I've never seen it return "the wrong value", i.e. I've never seen it say that a button was pressed when it was actually not pressed.
Gevater_Tod4711 Gevater_Tod4711

2012/9/23

#
Well, I'm not sure if I tested this the right way. So I must say I'm not sure if it returns the wrong value. Maybe it only seems so sometimes because of my code.
SPower SPower

2012/9/23

#
@Gevater It's probably not about your code, I also experience it as I said.
danpost danpost

2012/9/23

#
This code will work without regard to which mouse button is used.
// instance variables to be added
int mouseButtonsDown = 0;
int shotDelay = 0;
// code within the act method
if (Greenfoot.mousePressed(null))
{
    mouseButtonsDown++;
    if (shotDelay == 0) shoot();
}
if (Greenfoot.mouseClicked(null))
{
    mouseButtonsDown--;
    if (mouseButtonsDown == 0) shotDelay = 0;
}
if (mouseButtonsDown > 0)
{
    shotDelay = (shotDelay + 2) % 3;
    if (shotDelay == 0) shoot();
}
davmac davmac

2012/9/23

#
SPower, what do you mean? Can you give me some example of how to make getButton() return the wrong button? For me it always either returns the correct button, or 0.
SPower SPower

2012/9/24

#
Well, while I was updating my Paintshop scenario, I used the getButton method. Maybe it was because I was using a trackpad, but sometimes it returned 0 while I pressed the 'left' button.
davmac davmac

2012/9/24

#
Right, so it's not returning the wrong button - it's just returning 0, as we already established.
SPower SPower

2012/9/24

#
Ok, sorry for the mistake.
You need to login to post a reply.