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()) ?


1 2 3 4 5 6 7 8 | public void act() { ... mouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse.getButton() != 0 ) { //the gun should shoot but it does only sometimes. } ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 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(); } |