Monday, 15 July 2013

android - Saving a counter in libGDX -



android - Saving a counter in libGDX -

i developing game in libgx android. have counter on screen increment when touch something. in game can die, want save highest score player had. don't know begin with. new android game development. method when u die.

if (player.getbounds().overlaps(rock.getbounds()){ system.out.println("game over"); }

and here increment counter

if (player.getbounds().overlaps(food.getbounds()) { system.out.println("food"); counter++; }

thankyou

it sounds need preferences. provide platform independent way of saving little amounts of data. ideal stuff high scores.

call loadprefs() load storage , saveprefs() save them back.

public static preferences prefs; public static int counter; // variable want save //save shared preferences public static void saveprefs(){ prefs = gdx.app.getpreferences("game-prefs"); // name of prefs files prefs.putinteger("counter", counter); //counter counter wish save. "counter" name of varable saved in shared preferences prefs.flush(); //save preferences } //load shared preferences public static void loadprefs(){ prefs = gdx.app.getpreferences("game-prefs"); counter = prefs.getinteger("counter",0) //load counter, default 0 if not found }

android libgdx screen overlap bounds

No comments:

Post a Comment