Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. private Context mContext;
  2.  
  3. this.mContext = context;
  4.  
  5. SharedPreferences prefs = mContext.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE);
  6.  
  7. public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
  8. {
  9.  
  10. public static final int WIDTH = 856;
  11. public static final int HEIGHT = 480;
  12. public static final int MOVESPEED = -5;
  13. private long smokeStartTime;
  14. private long missileStartTime;
  15. private MainThread thread;
  16. private Background bg;
  17. private Player player;
  18. private ArrayList<Smokepuff> smoke;
  19. private ArrayList<Missile> missiles;
  20. private ArrayList<TopBorder> topborder;
  21. private ArrayList<BotBorder> botborder;
  22. private Random rand = new Random();
  23. private int maxBorderHeight;
  24. private int minBorderHeight;
  25. private boolean topDown = true;
  26. private boolean botDown = true;
  27. private boolean newGameCreated;
  28.  
  29. public static int HighScore = 0;
  30.  
  31. public static SharedPreferences prefs;
  32.  
  33. private String saveScore = "HighScore";
  34.  
  35. //increase to slow down difficulty progression, decrease to speed up difficulty progression
  36. private int progressDenom = 20;
  37.  
  38. private Explosion explosion;
  39. private long startReset;
  40. private boolean reset;
  41. private boolean dissapear;
  42. private boolean started;
  43. public static int highScore;
  44. private Context mContext; //Add this line <------
  45.  
  46.  
  47.  
  48. public GamePanel(Context context)
  49. {
  50. super(context);
  51. this.mContext = context; //Add this line <------
  52.  
  53. //Context pref;
  54. //SharedPreferences prefs = this.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE);
  55. //Your SharedPreferences are allready defined
  56. prefs = mContext.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE); //Add this line <---
  57. int oldScore = prefs.getInt("highScore", 0);
  58. int newScore = Player.getScore()*3;
  59. //update score only if new score is higher
  60. if(newScore > oldScore ){
  61. Editor edit = prefs.edit();
  62. edit.putInt("highScore", newScore);
  63. edit.commit();
  64. highScore = newScore;
  65. }
  66.  
  67. String spackage = "com.knight.myfirstgame";
  68.  
  69. HighScore = prefs.getInt(saveScore, 0);
  70.  
  71.  
  72. //add the callback to the surfaceholder to intercept events
  73. getHolder().addCallback(this);
  74.  
  75. //make gamePanel focusable so it can handle events
  76. setFocusable(true);
  77. }
  78.  
  79. context.getSharedPreferences(...)
  80.  
  81. this.getContext().getSharedPreferences(...)
  82.  
  83. import android.app.Activity;
  84. import android.content.SharedPreferences;
  85.  
  86. import static android.content.Context.MODE_PRIVATE;
  87.  
  88.  
  89. public class Preferences {
  90. public SharedPreferences preferences;
  91.  
  92. public void setPreferences(Activity activity, String key, String value){
  93. preferences = activity.getApplicationContext().getSharedPreferences("preferencestorage", MODE_PRIVATE);
  94. SharedPreferences.Editor preferencesEditor = preferences.edit();
  95.  
  96. preferencesEditor.putString(key,value);
  97. preferencesEditor.apply();
  98. }
  99.  
  100. public String getPreferenceValue(Activity activity, String key){
  101. SharedPreferences preferences = activity.getApplicationContext().getSharedPreferences("preferencestorage", MODE_PRIVATE);
  102. return preferences.getString(key,"");
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement