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?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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; } } |