我想制作离线日常礼物系统。我怎么能这样做。
date = new Date();
calendarG = new GregorianCalendar();
calendarG.setTime(date);
if(!prefs.contains("lastloginday"))
prefs.putInteger("lastloginday", calendarG.get(Calendar.DAY_OF_MONTH));
if (calendarG.get(Calendar.DAY_OF_MONTH) - 1 == prefs.getInteger("lastloginday")) {
prefs.putInteger("lastloginday", calendarG.get(Calendar.DAY_OF_MONTH));
prefs.putInteger("dailyCombo", prefs.getInteger("dailyCombo") + 1);
prefs.putInteger("Coin", prefs.getInteger("Coin") + prefs.getInteger("dailyCombo") * 25);
}else{
prefs.putInteger("lastloginday", calendarG.get(Calendar.DAY_OF_MONTH));
}
此代码在不同月份无用。
答案 0 :(得分:1)
你可以这样做:
Preferences preferences=Gdx.app.getPreferences("MyPref");
String LAST_LOGIN_DAY="lastloginday";
GregorianCalendar calendarG = new GregorianCalendar();
calendarG.setTime(new Date());
if(!preferences.contains(LAST_LOGIN_DAY)) {
//first day in App
preferences.putInteger(LAST_LOGIN_DAY, calendarG.get(Calendar.DAY_OF_YEAR));
preferences.flush();
}
if(preferences.getInteger(LAST_LOGIN_DAY)-1==calendarG.get(Calendar.DAY_OF_YEAR)){
//next loginday up to a year
updateValue(preferences,calendarG);
}else{
if(calendarG.get(Calendar.DAY_OF_YEAR)==1) {
// check for the 1st day of the year
boolean isLeap = calendarG.isLeapYear(calendarG.get(Calendar.YEAR));
if (isLeap && preferences.getInteger(LAST_LOGIN_DAY)==366 ) {
updateValue(preferences,calendarG);
}else if(preferences.getInteger(LAST_LOGIN_DAY)==365){
updateValue(preferences,calendarG);
}
else
preferences.putInteger(LAST_LOGIN_DAY,calendarG.get(Calendar.DAY_OF_YEAR));
}
else
preferences.putInteger(LAST_LOGIN_DAY,calendarG.get(Calendar.DAY_OF_YEAR));
}
这是更新方法:
public void updateValue(Preferences preferences,GregorianCalendar calendarG){
preferences.putInteger(LAST_LOGIN_DAY,calendarG.get(Calendar.DAY_OF_YEAR));
preferences.putInteger("dailyCombo", preferences.getInteger("dailyCombo",0) + 1);
preferences.putInteger("Coin", preferences.getInteger("Coin",0) + preferences.getInteger("dailyCombo",0) * 25);
preferences.flush();
}