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

2019/5/21

best score and the score in the end game doesn't appear,it stays zero needs help!

Tasya16 Tasya16

2019/5/21

#
  public void setPlayerHighScore(String s)
    {Label scoreBoardMsg = new Label("Your Score:" + s,35);
    Label highScoreMsg = new Label("Your Best:" + recordAndReturnHighScore(s),35);
addObject(scoreBoardMsg,getWidth()/2,getHeight()*1/2);
addObject(highScoreMsg,getWidth()/2,(getHeight()*1/2)+45);}
private String recordAndReturnHighScore( String s)
{String hs= null;
try { Path scoreFile = Paths.get("./scoreFile.txt");
if(Files.exists(scoreFile))
{ byte[] bytes = Files.readAllBytes(scoreFile);
hs = new String(bytes);
if(Integer.parseInt(s) > Integer.parseInt(hs)) { 
Files.write(scoreFile,s.getBytes());
hs = s;
}} else{Files.write(scoreFile,s.getBytes()); hs=s; }
} catch(IOException e){ System.out.println("IOException");}
return hs;
}
Tasya16 Tasya16

2019/5/21

#
public class Label extends Actor
{ 
   GreenfootImage msg;
   public Label(String s,int size) 
   {this(s,size,Color.WHITE);}
   public Label (String s,int size,Color c)
   { msg= new GreenfootImage(s,size,c,null);
       setImage(msg);}
    public void act() 
    {
        // Add your action code here.
    }    
}
Tasya16 Tasya16

2019/5/21

#
import greenfoot.*; /** * A simple score class. */ public class Score extends Actor { int score = 0; // the score field /** * Creates the object and its initial image. */ public Score() { updateImage(); } /** * Creates (or re-creates) the image of the object. */ private void updateImage() { setImage(new GreenfootImage(""+score, 30, Color.WHITE, new Color(0, 0, 0, 0))); } public int getScore() { return score; } /** * Adusts the score by <i>addAmt</i>. * * @param addAmt: the change in the score (negatives decrease the score) */ public void add(int addAmt) { score+=addAmt; updateImage(); } }
Tasya16 Tasya16

2019/5/21

#
public void endGame()
{ YouWin go = new YouWin(); 
    List objectslookingfor2 = getObjects(Kesehatan.class);
if (objectslookingfor2.size() == 0){go.setPlayerHighScore(Integer.toString(scoreBoard.getScore())); Greenfoot.setWorld(go);}
}
Tasya16 Tasya16

2019/5/22

#
Can anybody please help :(
Scorcher Scorcher

2019/5/22

#
I'll try and help, but I need more information. Where the code from your first comment located in? Where does the int s come from in your first comment? Is the int score from the Score class the global score? Is the Score class created in the world constructor or at a certain time in the program? -Whenever the Score class is created it will set int score to 0, so if not created at the beginning, it will just set the 0 when called (like if you create one at the end, it will be 0) Why isn't int score a static variable?
Super_Hippo Super_Hippo

2019/5/22

#
Why is the first code such a mess and not indented at all? You didn't say if the Score class works as intended. So is it showing the score correctly and only the Label class does not? You didn't show where you add the Score object to the world and not how you adjust its value (especially in case it does not work – check question above). Basically you should try to check which parts of the code are working and which specific part is not. And you should explain that. @Scorcher: The "int score" is not static, so it a) doesn't have to be set to 0 manually from the first world's constructor and b) so it can be used for several counters and not only the score. (Well, even though this probably isn't done here.)
You need to login to post a reply.