i get a terminal window with
at Miniship.act(Miniship.java:9)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
at Miniship.fire(Miniship.java:21)
at Miniship.act(Miniship.java:9)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
at Miniship.fire(Miniship.java:21)
at Miniship.act(Miniship.java:9)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
My code
import greenfoot.*;
import java.util.List;
/**
*/
public class Ship extends Actor
{
int speed = 5;
int shotCounter = 0;
private int delay = 0;
private final int MAX_DELAY = 13;
//private Bar bar;
/**
* Method act: move spaceship
*/
public void act()
{
move();
if(delay <= 0) {
if( Greenfoot.isKeyDown("space") )
{
fire();
Greenfoot.playSound("laser.wav");
delay = MAX_DELAY;
}
}
else {
delay--;
}
}
/**
* Fire the laser
*/
private void fire()
{
Shot shot = new Shot();
getWorld().addObject(shot, getX(), getY());
getWorld().addObject(shot, getX(), getY());
shot.setRotation(getRotation());
}
/**
* Method move: checks for keystrokes and applies the changes, then moves the ship.
* I applied a bit of slowing to the ship's speed (so it would be drifting to a stop)
*/
private void move()
{
int dz = 0;
if (Greenfoot.isKeyDown("d")) dz++;
if (Greenfoot.isKeyDown("a")) dz--;
setRotation(getRotation() + dz * 5);
int ds = -1;
if (Greenfoot.isKeyDown("w")) ds += 2;
speed += ds;
if (speed < 0) speed = 0;
if (speed > 90) speed = 90;
if (speed >= 200) move(speed / 100);
if (Greenfoot.isKeyDown("s")) ds += 5;
speed += ds;
if (speed < 5) speed = 5;
if (speed > 200) speed = 200;
if (speed >= 20) move(speed / 10);
}
}

