Hello. I am trying to add a scoreboard from an actor class (Player) to a World (GameOver) that the Player switches to. Basically, I am trying to set the world to GameOver from the Player class and adding a scoreboard, and it isn't working (there is no visible scoreboard when the GameOver world is switched to.
Here is the code for the Player class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends MazeCreature
{
public int food = 0;
public int lives = 5;
public int score = 0;
public int level = 0;
private Scoreboard score1;
public Player(int size)
{
getImage().scale(size,size);
int angle = Greenfoot.getRandomNumber(4)*90;
setRotation(angle);
}
public Player()
{
this(40);
}
public void act()
{
checkKeys();
if(canMove()){
moveForward();
}
Actor food1 = getOneIntersectingObject(Food.class);
if(food1!=null){
getWorld().removeObject(food1);
food = food+1;
}
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
Actor home = getOneIntersectingObject(Home.class);
if(home!=null){
level = level+1;
getWorld().addObject(new Lizard(), x, y);
Maze maze = (Maze)getWorld();
maze.addFood(5);
if(getX()==0){
home.setLocation(maze.getWidth()-1, maze.getHeight()-1);
} else {
home.setLocation(0,0);
}
}
Actor lizard = getOneIntersectingObject(Lizard.class);
if(lizard!=null){
lives = lives-1;
}
if(lives==0){
Greenfoot.setWorld(new GameOver());
score1 = new Scoreboard("Score", food+level*10);
getWorld().addObject(score1, 3, 3);
Greenfoot.stop();
}
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("left")){
setRotation(WEST);
}
if(Greenfoot.isKeyDown("right")){
setRotation(EAST);
}
if(Greenfoot.isKeyDown("up")){
setRotation(NORTH);
}
if(Greenfoot.isKeyDown("down")){
setRotation(SOUTH);
}
}
}
Here is the code for the Scoreboard class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
public class Scoreboard extends Actor
{
//Properties ...
private int score;
private String text;
public void act()
{
}
public Scoreboard(String label, int startingScore)
{
GreenfootImage img = new GreenfootImage(300,40);
img.setColor(Color.WHITE);
Font f=new Font("Comic Sans MS",Font.BOLD,24);
img.setFont(f);
img.drawString(label + ": " + startingScore, 5, 35);
setImage(img);
text=label;
score=startingScore;
}
public void changeScore(int howMuch)
{
score = score + howMuch;
GreenfootImage img = getImage();
img.clear();
img.drawString(text + ": " + score, 5, 35);
}
public int getScore()
{
return score;
}
}
Thank you, I really appreciate it.

