Hello! I am quite new to java and greenfoot and were tasked with adding a delay between bullets on a spaceship project.
This is the spaceship code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Spaceship here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Spaceship extends Actor
{
int speed = 5;
/**
* Act - do whatever the Spaceship wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
move();
shoot();
}
public void move()
{
if (Greenfoot.isKeyDown("a"))
setLocation(getX()-speed,getY());
if (Greenfoot.isKeyDown("d"))
setLocation(getX()+speed,getY());
if (Greenfoot.isKeyDown("w"))
setLocation(getX(),getY()-speed);
if (Greenfoot.isKeyDown("s"))
setLocation(getX(),getY()+speed);
}
public void shoot(){
if (Greenfoot.isKeyDown("space")){
Bullet bullet = new Bullet();
World world = getWorld();
world.addObject(bullet, getX(), getY());
}
}
}
And this is the bullet code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Actor
{
int speed = 7;
public Bullet()
{
setRotation(-90);
}
public void act()
{
// Add your action code here.
move(speed);
}
}