I am trying to get the world to go to a new level when the score reaches a certain number. I have the score counter working, but when it reaches the set number for the score limit, it continues to run the scenario. Any suggestions?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
public class ScoreHandler extends Actor
{
private static final String scoreLabel = "Score: ";
private static final String livesLabel = "Lives: ";
private static int score = 0;
private static int lives = 0;
private GreenfootImage box;
public ScoreHandler() {
box = new GreenfootImage (120, 60);
box.setColor(new Color (0f,0f,0f,0.5f));
box.fillRect (0,0, 200, 60);
updateStatus (0, 0);
}
public void act() {
ShooterWorld w = (ShooterWorld)getWorld();
score = w.getScore();
lives = w.getLives();
updateStatus (score, lives);
}
public void updateStatus (int score, int lives)
{
GreenfootImage img = new GreenfootImage (box);
img.setColor (Color.RED);
img.setFont(new Font("SANS_SERIF", 0, 16));
img.drawString(scoreLabel + score, 5, 12);
img.drawString(livesLabel + lives, 5, 30);
setImage (img);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class ShooterWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ShooterWorld extends World
{
/**
* Constructor for objects of class ShooterWorld.
*
*/
public int score = 0;
public int lives = 5;
private int level;
public boolean running, spirited;
public ShooterWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1, false);
prepare();
level = 0;
reset();
}
public void reset()
{
if (score == 25)
{
newLevel();
}
}
public void newLevel()
{
level++;
Greenfoot.setSpeed(44+level/2);
removeObjects(getObjects(null));
running = false;
ScoreHandler scorehandler = new ScoreHandler();
addObject(scorehandler, 36, 579);
scorehandler.setLocation(60, 574);
}
public void resetMovers()
{
running = false;
removeObjects(getObjects(Plane.class));
removeObjects(getObjects(Car.class));
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Car car = new Car();
addObject(car, 411, 575);
Plane plane = new Plane();
addObject(plane, 64, 51);
ScoreHandler scorehandler = new ScoreHandler();
addObject(scorehandler, 36, 579);
scorehandler.setLocation(60, 574);
}
public void addScore() {
score++;
}
public int getScore() {
return score;
}
public void loseLife() {
lives--;
}
public int getLives() {
return lives;
}
}