Hey, I'm making a game, and I'm wondering how to make a file and then edit/read it so I can store high scores. Any help on this will be appreciated.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Preferences; public class HighScores extends Actor { private Preferences prefs; private int highestScore = 0 ; private boolean prefsWorking = false ; public void addHighScore( int score){ /*If in an applet we don't have permision to use preferences and * a SecurityException will be thrown. If this happens we set * prefsWorking to false */ try { prefs = Preferences.userNodeForPackage(HighScores. class ); prefsWorking = true ; } catch (Exception e){ prefsWorking = false ; } if (prefsWorking){ //We now get the value from preferences highestScore = prefs.getInt( "yourgamename_highestscore" , 0 ); if (score > highestScore){ //If the score is higher than the highest score we put it's value as the new highest score prefs.putInt( "yourgamename_highestscore" , score); } } } } |
1 | prefs = Preferences.userNodeForPackage(billygame.SomeClass. class ); |