I'm having some trouble starting the next wave of enemies when the player reaches a certain score.
On level 1 I'm trying to get the second wave of enemies to appear when the player reaches 30 points. There's 3 enemies in wave 1, killing one of them gives the player 10 points so killing them all will give the player 30 points and starts wave 2.
But it gives me a "java.lang.NullPointerException" error on line 44 of the Level1 class when I have the code in the act method of the Level1 class...
My world Class:
Score class (by mik):
public class Level1 extends World
{
private Score score;
public Level1()
{
super(640, 640, 1);
stopMusic();
started();
addObject(new Player1(), 57,346);
Score score = new Score();
addObject(score, 573,19);
// spawn enemies for Wave 1
addObject(new ImageLabel("WAVE: 1"), 414, 20);
addObject(new RahikiLvl1(score), 379, 348);
addObject(new RahikiLvl1(score), 501, 62);
addObject(new RahikiLvl1(score), 541, 575);
//
}
public void act()
{
if (score.getValue() == 30)
{
wave2();
}
}
public void wave2()
{
// I need to add a code to remove the Wave:1 ImageLabel
addObject(new ImageLabel("WAVE: 2"), 414, 20);
addObject(new RahikiLvl1(score), 337, 559);
addObject(new RahikiLvl1(score), 432, 84);
addObject(new RahikiLvl1(score), 542, 348);
}
public void wave3()
{
// I need to add a code to remove the Wave:2 ImageLabel
addObject(new ImageLabel("WAVE: 3"), 414, 20);
addObject(new RahikiLvl1(score), 331, 61);
addObject(new RahikiLvl1(score), 408, 573);
addObject(new RahikiLvl2(score), 563, 233);
}
public void started()
{
Menu.Lvl1Music.playLoop();
}
public void stopped()
{
Menu.Lvl1Music.pause();
}
public void stopMusic()
{
if (Menu.TitleMusic.isPlaying()) Menu.TitleMusic.stop();
if (Menu.Lvl2Music.isPlaying()) Menu.Lvl2Music.stop();
if (Menu.Lvl3Music.isPlaying()) Menu.Lvl3Music.stop();
}
}
public class Score extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
/**
* Create a new counter, initialised to 0.
*/
public Score()
{
background = getImage(); // get image from class
value = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
