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

2019/12/6

How can I save a score and present it at the end screen?

TylerBlair TylerBlair

2019/12/6

#
This is how our counter is set up
import greenfoot.*; 
public class Counter extends Actor
{
    int score;
    private int time = 3600;    //60 seconds = 3600
   public Counter(){
        setImage(new GreenfootImage("Score: " + score + "\n Time: " + (time/60), 40, Color.WHITE, new Color (0, 0, 0, 0)));
    }
    
   public void act() 
    {
        countTime();
        setImage(new GreenfootImage("Score: " + score + "\n Time: " + (time/60), 40, Color.WHITE, new Color (0, 0, 0, 0)));
    }    
    
    
   private void countTime(){
    //Creates a timer at the top of the screen and stops it when it gets to a certain number - TYLER
         time--;
        if (time == 0)
        {
            Greenfoot.setWorld(new ForestDialogue1());
        }
    }
    
   public int getTime(){
       return time;
   }
 }
And this is how it is called inside our program to the world.
public class DungeonWorld extends World
{
   //Variables for spawnKnights()
   private int spawnTime = 80;    //How fast the enemies should appear in the world
   private int time = 0;           //Total time program has been running
   private int randomSide;         //Used to decide which side the enemy will spawn from
   
   Counter counter = new Counter(); //Initialize Counter object so that Enemy class can use it
   public Player slime = new Player();
   HealthBarDungeon healthbarDungeon = new HealthBarDungeon();

   GreenfootSound dungeonMusic = new GreenfootSound("SNES-Dungeon02_loop (online-audio-converter.com).mp3");
   private GreenfootImage bg = new GreenfootImage("dungeon.jpg");
   public DungeonWorld()
   {    
       super(1080, 580, 1); //Creates dimensions for world
        addObject(slime, 100, 450);  //Adds the Player on screen
        addObject(counter, 100, 75);       //Adds the time and score counter on screen
        addObject(healthbarDungeon, slime.getX(), slime.getY() - 70);
   }
    
   public void act(){
       if(counter.getTime() <= 3)   //Music stops once the timer reaches zero and transitions to next level
            dungeonMusic.stop();
       else
            dungeonMusic.play();
       scrollBackground();
       time++;
       spawnKnights();
       showGameOver();
   }
    
   private void scrollBackground()
   {
        GreenfootImage background = new GreenfootImage(getBackground());
        getBackground().drawImage(background, -1, 0);
        getBackground().drawImage(background, getWidth()-11, 0);
   }
   
   public void spawnKnights(){
    //At certain time intervals, an enemy is added on screen either on the left or right side of the world.
        if(time % spawnTime == 0){
            randomSide = Greenfoot.getRandomNumber(2);
            switch(randomSide){
                case 0: addObject(new Enemy(counter), 1080, 415); break;
                case 1: addObject(new Enemy(counter), 0, 415); break;
            }
        }
   }
   
   public Player getPlayer(){
       return slime;
    }
    
   public HealthBarDungeon getHealthBarDungeon(){
       return healthbarDungeon;
    }
   
   public Counter getCounter(){
       return counter;
    }
    
   public void showGameOver(){
       if(getHealthBarDungeon().getHealth() == 0){
           dungeonMusic.stop();
           Greenfoot.setWorld(new GameOver());
        }
   }
}
Is there a way to add just the score to a final screen, which the world is called EndScreen, without reseting the score or starting or showing the timer again?
danpost danpost

2019/12/6

#
I do not see where any EndScreen world is being created.
You need to login to post a reply.