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

2019/1/8

score display

aa124 aa124

2019/1/8

#
I have a separate screen for when the game ends. I would like to display the score in my end screen. How can I do it?
public class Counter extends Actor. I want it to appear on my GameOverScreen
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, greenfoot.Color.RED, greenfoot.Color.BLACK));
    } 
     
    public void addScore()
    {
        score++;
    }
}
public class GameScreen extends World
{
    private int delay = 0;
    Counter counter = new Counter();
    public int lifeCounter = 3;
     
    private Lives lives1;
    private Lives lives2;
    private Lives lives3;
    /**
     * Constructor for objects of class GameScreen.
     * 
     */
    public GameScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 
        
        prepare();
    }
    
    public Counter getCounter()
    {
      return counter;  
    }
public class Laser extends Actor
{
public void act() 
    {
        if(!toRemove)
        {
            setLocation(getX()+vx,getY());
            Actor actor=getOneIntersectingObject(Enemy.class);
            if(actor!=null)
            {
                GameScreen gameScreen = (GameScreen)getWorld();
                Counter counter = gameScreen.getCounter();
                counter.addScore();
                ((Enemy)actor).destroyed();
                Greenfoot.playSound("Explosion.wav");
            }
}
danpost danpost

2019/1/8

#
aa124 wrote...
I have a separate screen for when the game ends. I would like to display the score in my end screen. How can I do it? << Codes Omitted >>
I do not see where you go to your game over screen.
aa124 aa124

2019/1/8

#
public void adjustLives(int amt)
    {
        lifeCounter += amt;
        if (lifeCounter < 3) 
        {
            removeObject(lives3);
        }
        if (lifeCounter < 2) 
        {
            removeObject(lives2);
        }
        if (lifeCounter < 1)
        {
            removeObject(lives1);
            Greenfoot.delay(60);
            Greenfoot.setWorld(new GameOverScreen());
        }
    }
danpost danpost

2019/1/8

#
Okay, where do you call adjustLives from? (show code)
aa124 aa124

2019/1/8

#
I call it in the enemy class
public void act() 
    {
        if(!toRemove)
        {
            move();
            if (getX() < 10)
            {
                GameScreen world = (GameScreen)getWorld();
                world.removeObject(this);
                world.adjustLives(-1);
            }
        }
        else
        {
            getWorld().removeObject(this);
        }
    }    
}
danpost danpost

2019/1/8

#
Okay. In the adjustLives method, replace line 16 (as shown above) with the following:
GameOverScreen gos = new GamOverScreen();
gos.addObject(counter, 300, 200);
Greenfoot.setWorld(gos);
aa124 aa124

2019/1/8

#
THANKS DANPOST
You need to login to post a reply.