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

2020/2/26

Need help with highscore system

BalanZeD BalanZeD

2020/2/26

#
Hey, I'm new to Greenfoot and need to finish my Game. I would like to add a highscore which is saved and everytime a new highscore is achieved the backround should be switched. that is all it should do and i really need your help. Thanks
Archerfish Archerfish

2020/2/26

#
Hey BalanZeD! I would recommend using a "public static variable", this allows for a value to be kept and accessed--even when the world gets restarted (HOWEVER, note that if the code gets recompiled, then the value will be reset). To declare a public static variable, do the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
import greenfoot.*;
 
//WORLD CLASS
public class MyWorld extends World
{
    //DECLARATION AND INITIALIZATION
    public static int highScore = 0;
 
    //COUNSTRUCTOR
    public MyWorld()
    
    }
}
After each game ends, compare the "final game" score to the current "high score." If the "final game score" is greater than the current "high score," then set it as the new "high score" (it is also at this point that the background should be change).
1
2
3
4
5
if(finalGameScore > highscore)
{
   highscore = finalGameScore;
   //change image here
}
NOTE: If one desires to access the public static variable "highScore" in another class, then they must use the following format: name of the world the variable was declared in, followed by a ".", followed by the name of the variable. In this case:
1
MyWorld.highScore
BalanZeD BalanZeD

2020/2/26

#
thank you so much for your reply. I will try it out! If i would need some more help i would appreciate it if you could help me again. ;D
BalanZeD BalanZeD

2020/2/27

#
Hey Archerfish! It doesnt work or I'm just dumb. I uploaded my scenario because I need help with a different thing as well. I wanted to ask you if you could do the highscore thing and maybe you have a solution for my other problem as well. I'm not into that stuff here. It would be really great if you could help me. The other problem is mentioned in my scenario which i uploaded. Thanks Sir!
BalanZeD BalanZeD

2020/2/27

#
I'm really sorry for wasting your time. I have a present for you as well if you could help me here ;D
You need to login to post a reply.