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

2020/12/9

hbhk

NewbJava NewbJava

2020/12/9

#
import greenfoot.*;  
 
public class MyWorld extends World // focuses on displaying of the score 
{ 
    static Actor scoreDisplay;        // all static variable because they rest once the game is finished 
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    
      
    public MyWorld() // screen size and sets variable values, 
    {    
         
        super(600, 400, 1);   
         
        scoreDisplay = new SimpleActor();
        highScoreDisplay = new SimpleActor();
        score = -1;
        highScore = -1;
        adjustScore(1); 
         
         
         
         
          
           
          
    }    
    

    
    public MyWorld(int w, int h, int c) 
    {
        super(w, h, c);
    }
       
     
    static void adjustScore(int trial) // score resseting and high score being saved for every round and being replaced if score is largr than high score 
    {
        score += trial;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, new Color(0, 0, 0, 0));
        scoreDisplay.setImage(img);
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, new Color(0, 0, 0, 0));
            highScoreDisplay.setImage(img);
        }
    }
     
    public void act() // getting from the MeteorsWorld() method in the sub class 
    {  
        Greenfoot.setWorld(new MeteorsWorld()); 
        
        
         
    }   
    
   
}
RcCookie RcCookie

2020/12/9

#
Well, you didn’t write the problem, but you never add highScoreDisplay to any world. Also, I don’t see a reason to have these field static.
danpost danpost

2020/12/9

#
RcCookie wrote...
Well, you didn’t write the problem, but you never add highScoreDisplay to any world. Also, I don’t see a reason to have these field static.
Classes that extend this class add the highScoreDisplay to the worlds. Fields are static to retain scores across world levels, resets and replays.
You need to login to post a reply.