2
\$\begingroup\$

I am trying to make a game which there are many stages. For example, If users are done stage 3 of the game, and then turn off the game, they can play the game at stage 3 anytimes.

simply, I tried to make this logic, so when you press space bar, the level goes up. However, after turning off and on, lev will start at 0. I guess when program are ruining every single times, the lev is initialized 0.

I tried to use preference.

please let me know solutions..

AppPreferences.java

public class AppPreferences {

   private static final String PREFS_NAME = "Adventure";
   private static final String PREF_LEVEL = "Level";
   private Preferences preferences;


   protected Preferences getPrefs(){
    if (preferences == null)
        preferences = Gdx.app.getPreferences(PREFS_NAME);
       return preferences;
}

public void setPrefLevel(int level){
    getPrefs().putInteger(PREF_LEVEL,level);
    getPrefs().flush();
}

public int getPrefLevel(){
    return getPrefs().getInteger(PREF_LEVEL);
}

}

PlayScreen.java

public class PlayScreen implements Screen {
 int lev=0;


 public void render(float delta) {

     if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){

        advanture.getPreferences().setPrefLevel(i);
        System.out.println(advanture.getPreferences().getPrefLevel()+"!!!!!!!!!");
        i++;
   }

 }
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You don't seem to be assigning the lev variable anywhere. In the top of your show method you can put something like this:

@Override
public void show() {
    lev = advanture.getPreferences().getPrefLevel();
    // do other stuff like load level from number
}

This should make the level start at the loaded level. Your code would then look something like this:

public class PlayScreen implements Screen {
    int lev = 0;

    @Override
    public void show() {
        lev = advanture.getPreferences().getPrefLevel();
        // do other stuff like load level from number
    }

    public void render(float delta) {
        if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
            lev++; // Increase level by 1
            advanture.getPreferences().setPrefLevel(lev); // Save level
            System.out.println(lev+"!!!!!!!!!"); // Print level
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ thanks a lot for two times, it is working. if you do not mind, could you please explain what show() method is for ?? \$\endgroup\$
    – DHC
    Commented Dec 31, 2017 at 0:12
  • \$\begingroup\$ @DHC the show() method runs when you show your screen. So if you do setScreen(new PlayScreen()) it will run the show method on PlayScreen. Use it to set up and initialize variables. \$\endgroup\$
    – Charanor
    Commented Dec 31, 2017 at 0:42
0
\$\begingroup\$

You need to look into persistent storage e.g. a database or similar. Since your level variable is 0, every time you instantiate your program the level will be 0. You should read the level from persistent storage for the desired result.

If you deploy for Android, you could read or write to a file or an embedded database. If you target desktop PC:s you could use a database of any kind. Just store the level for the game in the database and then read it when the game starts again.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Database is overkill for this. A simple serialization format (Json, xml, binary) is sufficient. \$\endgroup\$ Commented Dec 30, 2017 at 23:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .