The objective of the game is to use a turret to shoot cannonballs to hit a target. My scoreboard should display the angle the turret it shooting at, the power it is shooting at, and the number of missed times that the cannonball was not able to hit the target.
I got the power and the angle to work and show up on the statusboard, but the number of misses will not show up. If i pause the game and check manually the number of misses, i can get it -but it wont show up on the status board.
I did some editing and now the game just stops a second after i shoot the cannonball and a nasty "Greenfoot: Terminal Window java.lang.NullPointerException error shows up.. D:
StatusBoard class:
CannonBall Class
I'm not sure if you need this, because this part actually works, but this is the
Turret Class
What am i doing wrong? D: Thanks in advanced, guys.
public class StatusBoard extends Actor
{
protected GreenfootImage background = null;
public void act()
{
// Add your action code here.
}
public StatusBoard()
{
background = new GreenfootImage (100, 180);
background.setColor(new java.awt.Color(255, 255, 255, 200));
background.fillRect(0,0,150,60);
setImage(background);
}
public void setValues (int angle, int power, int misses)
{
GreenfootImage img = new GreenfootImage (background);
img.setColor (java.awt.Color.BLACK);
img.drawString("Angle: " + angle, 10, 20);
img.drawString("Power: " + power, 10, 35);
img.drawString("Missed: " + misses, 10, 50);
setImage(img);
}
}
public class CannonBall extends Actor
{
public static final int GRAVITY = 2;
protected int angle = 0;
protected int power = 100;
protected int misses = 0;
protected StatusBoard statusBoard;
protected int xSpeed = 0;
protected int ySpeed = 0;
public void act()
{
// Add your action code here.
move(1);
setLocation (getX() + xSpeed, getY() - ySpeed);
ySpeed = ySpeed - GRAVITY;
destroyCannonBall();
statusBoard.setValues (angle, power, misses);
}
public CannonBall (StatusBoard board)
{
statusBoard = board;
}
public CannonBall (int angle, int power)
{
power = (power * 10) / 25;
xSpeed = (int)(power*Math.cos(Math.toRadians(angle)));
ySpeed = (int)(power*Math.sin(Math.toRadians(angle)));
}
//getting cannon ball to dissapear once its reached the edge of the world
public void destroyCannonBall()
{
FodderWorld fodderWorld = (FodderWorld)getWorld();
if (getY() > fodderWorld.getHeight())
{
fodderWorld.removeObject(this);
fodderWorld.gainMiss();
}
}
}
public class Turret extends Actor
{
protected int angle = 0;
protected int power = 100;
protected int misses = 0;
protected StatusBoard statusBoard;
public void act()
{
// Add your action code here.
aimTurret();
statusBoard.setValues (angle, power, misses);
fireCannonBall();
}
public Turret()
{
setRotation(300);
}
public Turret(StatusBoard board)
{
statusBoard = board;
}
//This method will allow the player to aim the turret
public void aimTurret()
{
if (Greenfoot.isKeyDown ("left") && (angle < 90))
{
angle = angle+1;
setRotation(-angle);
}
if (Greenfoot.isKeyDown ("right") && (angle > 0))
{
angle = angle-1;
setRotation(-angle);
}
if (Greenfoot.isKeyDown ("up") && (power < 100))
{
power = power+1;
}
if (Greenfoot.isKeyDown("down") && (power > 0))
{
power = power-1;
}
}
public void fireCannonBall()
{
if (Greenfoot.isKeyDown("space") && noCannonBalls())
{
World cannonWorld = getWorld();
CannonBall ball = new CannonBall (angle, power);
cannonWorld.addObject(ball, getX(), getY());
}
}
public Boolean noCannonBalls ()
{
{
World cannonWorld = getWorld();
java.util.List balls = cannonWorld.getObjects(CannonBall.class);
return balls.isEmpty();
}
}
}

