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:
/**
* 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;
}
/**
* 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);
}
}
}
