There are two basic ways to accomplish this: the first is reading and writing files which will work in the Greenfoot application and in jar form, but not on the Greenfoot site due to security issues; the other is using the storage available through the use of the UserInfo class. You should be able to import a ScoreBoard class that can be used to display some of the data the class stores ('Edit>Import class...' from the menubar).
This is how I have it written in the program and it want compile. What I am trying to do is make the software keep record of the ten names and highest score.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
/**
* The ScoreBoard is used to display results on the screen. It can display some
* text and several numbers.
*
* @author M Kolling
* @version 1.0
*/
public class ScoreBoard extends Actor
{
public static final float FONT_SIZE = 48.0f;
public static final int ITALIC = 2;
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public static final int PLAYERS = 10;
public static final int SCORE = newScore;
/**
* Create a score board with dummy result for testing. Create a score board for the final result.
*
public Counter getScoreCounter();
{
return scoreCounter;
((WorldName)getWorld).getScoreCounter().add(10);
// where 'getScoreCounter' is as follows (in your world class, which I have named 'World')
((Counter)getWorld().getObjects(Counter.class).get(0)).add(10);
makeImage("Game Over", "Score: ", score);
addObject( new Player(theScore), x, y ); // (x,y) is the starting position of the Player object.
myScore = sb;
}
/**
* Make the score board image.
*/
private void makeImage(String title, String prefix, int score)
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(new Color(255,255,255, 128));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(255, 0, 0, 128));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(ITALIC, FONT_SIZE);
image.setFont(font);
image.setColor(Color.BLUE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
{
if (UserInfo.isStorageAvailable());
UserInfo myInfo = UserInfo.getMyInfo();
if (newScore > myInfo.getScore())
{
myInfo.setScore(newScore);
myInfo.store(); // write back to server
}
}
}
It appears you imported the Scoreboard class and then edited it, ruining the basic structure of the class.
(1) the field 'scoreCounter' field is not declared or initialized anywhere (not sure if that method belong in this class)
(2) only the first statement (the 'return...') is reachable in the 'getScoreCounter' method (see note parenthesized in (1))
(3) the last block (starting '{ if(UserInfo.isStorageAvailable())...' is not within a method
(4) the 'SCORE' field is being set to 'newScore', yet 'newScore' seems to come from nowhere (I think you delected a constructor declaration for the block referred to in (3) whose argument is 'newScore')