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

2014/12/2

GameOver Screen Help

hdeforest hdeforest

2014/12/2

#
I am trying to get my gameOver screen to show the correct score. I currently have the correct score counting, I have the scoreBoard to come up, but I can get the correct score in the scoreBoard. Here is my code for the space class. The ???? at the bottom is where I need to put my score I believe....
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * Space. Something for rockets to fly in.
 * 
 * @author Michael Kolling
 * @version 1.1
 */
public class Space extends World
{
    private Counter scoreCounter;
    private int startAsteroids = 3;

    public Space() 
    {
        super(600, 400, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        createStars(300);

        Rocket rocket = new Rocket();
        addObject(rocket, getWidth()/2 + 100, getHeight()/2);

        addAsteroids(startAsteroids);

        scoreCounter = new Counter("Score:");
        addObject(scoreCounter, 60, 380);

        Explosion.initializeImages();
        ProtonWave.initializeImages();
    }

    /**
     * Add a given number of asteroids to our world. Asteroids are only added into
     * the left half of the world.
     */
    private void addAsteroids(int count) 
    {
        for(int i = 0; i < count; i++) 
        {
            int x = Greenfoot.getRandomNumber(getWidth()/2);
            int y = Greenfoot.getRandomNumber(getHeight()/2);
            addObject(new Asteroid(), x, y);
        }
    }

    /**
     * Crete a given number of stars in space.
     */
    private void createStars(int number) 
    {
        GreenfootImage background = getBackground();             
        for(int i=0; i < number; i++) 
        {
            int x = Greenfoot.getRandomNumber( getWidth() );
            int y = Greenfoot.getRandomNumber( getHeight() );
            int color = 120 - Greenfoot.getRandomNumber(100);
            background.setColor(new Color(color,color,color));
            background.fillOval(x, y, 2, 2);
        }
    }

    public void countScore(int count)
    {
        scoreCounter.add(count);
    }

    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        addObject(new ScoreBoard(?????), getWidth()/2, getHeight()/2);
    }
}
danpost danpost

2014/12/2

#
You probably have a 'getValue' method in the counter class and can use 'scoreCounter.getValue()'.
You need to login to post a reply.