Yeah.. that was just the only way i knew without a for loop and a delay...


1 2 3 4 5 6 | public void act() { setLocation(getX() + speed, getY()); checkBoundaries(); destroyEnemies(); } |
1 | move(speed); |
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 29 30 31 32 33 34 | public void act() { checkKeyPress(); } /** * This will check to see if a key has been pressed. If the left arrow is pressed, the turret * will rotate counterclockwise. If the right arrow is pressed, the turret will turn clockwise. */ // add instance field public int rotation = 0 ; // the method public void checkKeyPress() { if (Greenfoot.isKeyDown( "left" ) && rotation >- 90 ) { turn(- 5 ); rotation -= 5 ; } if (Greenfoot.isKeyDown( "right" ) && rotation < 90 ) { turn( 5 ); rotation += 5 ; } if (Greenfoot.isKeyDown( "space" )) { bullet bullet = new bullet(); bullet.setRotation(rotation); getWorld().addObject(bullet, getX(), getY()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 | // add this instance field private boolean spaceDown; // replace lines 28 on with if (!spaceDown && Greenfoot.isKeyDown( "space" )) { spaceDown = true ; bullet bullet = new bullet(); bullet.setRotation(rotation); getWorld().addObject(bullet, getX(), getY()); } if (spaceDown && !Geenfoot.isKeyDown( "space" )) spaceDown = false ; } |