简体   繁体   中英

How to initialize preference value once in LibGDX?

I'm using preferences to save the sound settings in my game as a boolean value. However, when I first start the game, the boolean initializes to false (sound off), because I'm not initializing it elsewhere. I could initialize it to true in the create method, but then the game would just start with sounds on every time you start the game, and that would just defeat the purpose of preferences.

Otherwise it works fine, it's just that I want the boolean to initialize to true the first time you start the game and not every time you restart it.

Is there a way to do this with preferences or do I have to use some other kind of saving method?

Note: this is a desktop application

    public Preferences getPreferences() {
        if (preferences == null) {
            preferences = Gdx.app.getPreferences("myPrefs");
        }
        return preferences;
    }

    private void generatePreferences() {
        getPreferences().clear();
        getPreferences().putBoolean("soundEnabled", true).flush();
        getPreferences().putBoolean("notFirstLaunch", true).flush();
    }

    public void loadPreferences() {
        if (!getPreferences().getBoolean("notFirstLaunch")) {
            generatePreferences();
        } else {
        //read the prefs and do your stuff
        }
    }

I would suggest you a slightly different approach:

Firstly, I think that the perfect place to initialize prefs is create method of the main game class (the one that extends Game ):

public void create () {
    Prefs.initPrefs();
    ....other initialization.... 
}

Then, initPrefs method looks like:

private static final String MUSIC_ON = "music_on";
private static final String LANG = "lang";    

public static void initPrefs() {
    boolean needChange = false;
    if (!pref.contains(MUSIC_ON)) {
        pref.putBoolean(MUSIC_ON, true);
        needChange = true;
    }

    //if no lang - select system default
    if (!pref.contains(LANG)) {
        String language = Locale.getDefault().getLanguage();
        pref.putString(LANG, language);
        needChange = true;
    }
    if (needChange) {
        pref.flush();
    }
}

And finally to toggle the music:

 public static boolean isMusicOn() {
     return pref.getBoolean(MUSIC_ON);
 }

 public static void toggleMusic() {
     pref.putBoolean(MUSIC_ON, !isMusicOn());
     pref.flush();
 }

I know this is a few years old now but just in case anyone else was wondering.

I think what you need to do is add a default value to your getBoolean() method without calling flush() .

In my game i have a method called isSoundOn() which i call when my sound button is created. The very first time you start your game after installing it, you probably wont have saved a preference which means that the method below has to default to something. if you add true to the getBoolean method then your game should initialize to true.

public boolean isSoundOn() {
    return preferences.getBoolean("soundOn", true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM