This site requires JavaScript, please enable it in your browser!
Greenfoot back
Tommy99
Tommy99 wrote ...

2014/5/3

Problem With Adding a Scoreboard to the World

Tommy99 Tommy99

2014/5/3

#
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.
danpost danpost

2014/5/3

#
You are adding the scoreboard to the Maze world and changing worlds to the GameOver world.
Tommy99 Tommy99

2014/5/3

#
Do you know how I can add the scoreboard to the GameOver world?
danpost danpost

2014/5/3

#
Change the last 'if' block in your 'act' method to this:
if(lives==0){
    World gameover = new GameOver(); // create game over world
    gameover.addObject(new Scoreboard("Score', food+level*10), 3, 3); // add scoreboard
    Greenfoot.setWorld(gameover); // activate game over world
    Greenfoot.stop(); // tell greenfoot to stop
}
You may have to adjust where the scoreboard is placed (the dimensions of that world may be different from that of the one you had it in).
Tommy99 Tommy99

2014/5/3

#
It worked perfectly! Thank you!
You need to login to post a reply.