I have been trying to get my Player to shoot at intervals but I am not sure how i would ideally like it if I could shoot multiple bullets then have a reload time, currently, I can only have 1 bullet on the screen at once and I am not sure why.
Player Code:
Bullet Code:
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /** * Act - do whatever the robot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { fireOnCommand(); gunReloadTime = 20 ; if (Greenfoot.isKeyDown( "left" )) { turn(- 5 ); } if (Greenfoot.isKeyDown( "right" )) { turn( 5 ); } if (Greenfoot.isKeyDown( "up" )) { move( 5 ); } if (Greenfoot.isKeyDown( "down" )) { move(- 5 ); } } public void fireOnCommand() { if (Greenfoot.isKeyDown( "space" )) { World myWorld = getWorld(); myWorld.addObject(bullet, 0 , 0 ); bullet.setLocation(getX(), getY()); bullet.setRotation(getRotation()); } } /** * Set the time needed for re-loading the rocket's gun. The shorter this time is, * the faster the rocket can fire. The (initial) standard time is 20. */ public void setGunReloadTime( int reloadTime) { gunReloadTime = reloadTime; } |
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 | /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends bullets { public int speed = 10 ; /** * Act - do whatever the missile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move( 15 ); Actor Robber = getOneIntersectingObject(Robber. class ); if (isTouching(Robber. class )) { getWorld().removeObject(Robber); getWorld().addObject( new Robber(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); getWorld().removeObject( this ); } } } |