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

2014/2/4

Why doesn´t appear my scoreboard

martijn13039 martijn13039

2014/2/4

#
hey if try to make a scoreboard if the game is over. If I compile there is nothing wrong but if the games end there whil not come a scoreboard here are my codes
import greenfoot.*;  


public class BackGround extends World
{ 
    private Counter counter = new Counter();  
    private concierge user = new concierge(counter);  
    
    private ScoreBoard board;

       public BackGround()
    {    

        super(800, 600, 1); 

        prepare();
        

    }

        public void gameOver() 

    { 
        removeObject(user);  
          
       
        Greenfoot.stop();

    }  

       public void prepare()  
    {     
        counter = new Counter();      
        user = new concierge(counter);  
        addObject(counter, 54, 23);  
        addObject(user, 120, 80);  
        Timer timer = new Timer();
        addObject(timer, 766, 28);
    }  

}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}

import greenfoot.*;  
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;


public class ScoreBoard extends Actor
{
    public static final float FONT_SIZE = 48.0f;
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    
    
     
    public ScoreBoard()
    {
        this(100);
    }

    /**
     * Create a score board for the final result.
     */
    public ScoreBoard(int score)
    {
        makeImage("Game Over", "Score: ", score);
    }

    /**
     * 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(0, 0, 0, 128));
        image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString(title, 60, 100);
        image.drawString(prefix + score, 60, 200);
        setImage(image);
    }
}
danpost danpost

2014/2/4

#
Is not something missing from the 'gameOver' method located in your Background class (is that all you are doing when the game ends)? It is no wonder the ScoreBoard object is not showing. I do not see one being added to the world anywhere (or being created, for that matter).
martijn13039 martijn13039

2014/2/4

#
Hey danpost, I see what you mean but how would ad or create the score. I try with att to world but it doens't work I hope you can give some example
martijn13039 martijn13039

2014/2/6

#
Danpost, could be the problem the code greenfoot stop in line 27 from background, is there no more time for showing the scoreboard?
danpost danpost

2014/2/7

#
I could not say. I do not know what changes you have made. I do not know from where you are calling the 'gameOver' method or, even, if you are calling it at all. I do not know what other code you may be executing around that call, if you do have it. etc.
martijn13039 martijn13039

2014/2/7

#
I have a method timer, so the game stops is "gameOver" after 60 seconds, but I still think I must add something to the world but how?
martijn13039 martijn13039

2014/2/8

#
Danpost I have solved the problem, it was just very easy to solve it. I have add this code by gameover (in backGround class).
addObject(new ScoreBoard(), getWidth()/2, getHeight()/2);
But there is now a new problem, if the game finished the scoreboard wil stay on 100 and won't go up or down. Have you a solution?
danpost danpost

2014/2/8

#
Use the other constructor in the ScoreBoard class to instantiate it ('new ScoreBoard(int)'). The int value should be the final score to be passed to the new scoreboard object.
martijn13039 martijn13039

2014/2/8

#
Danpost do you mean this, because now I get an error "class expect" bij ScoreBoard(int)
 public void gameOver() 

    { 
        removeObject(user);  
        addObject(new ScoreBoard(int), getWidth()/2, getHeight()/2);
       
        Greenfoot.stop();

    }  
danpost danpost

2014/2/8

#
danpost wrote...
The int value should be the final score to be passed to the new scoreboard object.
That means: 'new ScoreBoard(counter.getValue())'.
You need to login to post a reply.