I have a score counter and lives counter. Both use the Score code but I can't seem to get the lives counter to work properly. It will compile but when the player is hit I get an error 'NullPointerException at Player.act(Player.java:31)
at greenfoot.core.Simulation.actActor(Simulation.java:565)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
at greenfoot.core.Simulation.runContent(Simulation.java:213)
at greenfoot.core.Simulation.run(Simulation.java:203)
' error rather than the lives counter updating. Here is my code. What is wrong with it?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Actor
{
/**
* creates integer to hold values for missile fire rate
*/
private int canShoot = 0;
private int count = 3;
private Score lives;
public void act()
{
/**
* move around and fire missiles, destroyed by enemies, recharges missile fire
*/
moveUp();
moveDown();
moveLeft();
moveRight();
fire();
destroyed();
canShoot--;
if(count >= 0);
{
getWorld().addObject(new Lives3(), 245, 20);
}
if (count <= -1);
{
getWorld().addObject(new Lives2(), 245, 20);
}
if (count <= -2);
{
getWorld().addObject(new Lives1(), 245, 20);
}
if (count <= -3);
{
getWorld().addObject(new Lives0(), 245, 20);
}
}
private void moveUp()
{
/**
* if up button is pressed move ship up
*/
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY() - 3);
}
}
private void moveDown()
{
/**
* if down button is pressed move ship down
*/
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY() + 3);
}
}
private void moveLeft()
{
/**
* if left button is pressed move ship left
*/
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX() - 3, getY()) ;
}
}
private void moveRight()
{
/**
* if right button is pressed move ship right
*/
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX() + 3, getY() );
}
}
public void fire()
{
/**
*if space button is pressed spawn missile below player, play missile sound, sets fire rate,
*if v is pressed allow "unlimited" fire rate
*/
if (Greenfoot.isKeyDown("space") && (canShoot <= 0))
{
getWorld().addObject(new Missile(), getX()+ 5, getY());
canShoot = 45;
Greenfoot.playSound("Missile.wav");
if (Greenfoot.isKeyDown("v"))
{
canShoot = 0;
}
}
}
public void destroyed()
{
/**
* if hit by enemy - remove from game, create explosion image, create game over image,
* play explosion sound, delay game, remove explosion, play game over sound
*/
if (getOneIntersectingObject(Enemy.class) != null)
{
getWorld().addObject(new Explosion(), getX(), getY());
lives.add(-1);
getWorld().removeObject(this);
Greenfoot.playSound("Explosion2.wav");
if (count >=1)
{
setLocation(20 , 200);
}
else if (count <=0)
getWorld().addObject(new GameOver(), 400, 300);
{Greenfoot.playSound("Gameover.wav");
}
}
}
public Player(Score lifeCount)
{
lives = lifeCount;
lives.add(3);
}
}

