Hello Greenfoot Community,
I am trying to make a counter that displays the highest score(or in my case best time) achieved in my game. I have a variable called bestTime in my world class that stores the high score on my game. I tried not initializing it in the beginning and then initializing it after the player wins the game but everytime I restart the game it displays the same value for high score even if I get a better score. I guess since the default initialization for an int is 0 it is still initializing as 0 in the beginning. Can anybody help me?
Here is the code for my 'world' class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.*; /** * This is the GameWorld class. This class represents the world in which the game takes place. * At the moment this is the only gameworld class that has code in it. In the future I will have different levels which are the other GameWorlds. * * @author (Glenn Price) * @version (Version 0.02) */ public class GameWorld1 extends World//Class heading { private int spawnCounter;//the rate at which enemies spawn in the world. private int bestTime; private String bestTimeHolder; public static final int ORIGINAL_BEST_TIME = 36000; public static final String ORIGINAL_BEST_TIME_HOLDER = "Nobody"; private Player player1; private Counter keyCounter; private Counter scoreCounter; private Counter highScoreCounter; /** * Constructor for objects of class GameWorld. * */ public GameWorld1()//construcotr sets the spawnCounter to 100, populates the world, and starts the game { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 800, 1); spawnCounter = 100; populateWorld(); Greenfoot.start(); } public void populateWorld()//creates the player, all of the keys, and one virus. The player and the original virus start in the same spot but the keys are spawned in random locations which makes the game different every time. { JOptionPane.showMessageDialog(null, "Instructions: \n \n Arrow Keys: Movement \n Q Key: fire arrow \n W Key: drop bomb \n Spacebar: pick up key \n Pick up all 9 keys to win. \n Be Careful!!! Bombs will destroy everything in range including keys and yourself"); String inputstr = JOptionPane.showInputDialog("Hello, thank you for playing 9 keys.\n What is your Name?"); player1 = new Player(inputstr); JOptionPane.showMessageDialog(null, "Nice to meet you" + " " + inputstr + "! \nI am sorry to tell you this but you are trapped in my system and the only way to get out is to collect the 9 keys in this world. \nViruses will try to attack you but I have equipped you with weapons that will aid you on your quest. \nGood luck!"); Greenfoot.start(); addObject(player1, 30, 30); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); addObject(new Virus(), 110, 110); keyCounter = new Counter("Keys Found: "); scoreCounter = new Counter(player1.getName() + "'s Time: "); highScoreCounter = new Counter("Best Time: " + bestTimeHolder + ": "); if(bestTime == 0) { highScoreCounter.setValue(ORIGINAL_BEST_TIME); bestTimeHolder = ORIGINAL_BEST_TIME_HOLDER; } else { highScoreCounter.setValue(bestTime); } addObject(keyCounter,63, 15 ); addObject(scoreCounter,63,40); addObject(highScoreCounter,95,65); } public void act()//makes one virus spawn in a random location for every 100 calls of the act method { if(spawnCounter == 0) { addObject(new Virus(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 ); spawnCounter = 100; } spawnCounter--; keyCounter.setValue(player1.getKeysFound()); scoreCounter.add(1); if(keyCounter.getValue() ==9) { if(scoreCounter.getValue() < highScoreCounter.getValue()) { bestTime = scoreCounter.getValue(); bestTimeHolder = player1.getName(); } Greenfoot.stop(); JOptionPane.showMessageDialog(null, "YOU WIN!"); } } public void clearHighScores() { bestTime = 0; bestTimeHolder = null; } }