2017-05-28 45 views
-1

我想做一個離線的日常禮品系統。我該怎麼做。我如何在LibGDX上做日常禮品屏幕

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

如果有人有什麼偏好得到的持有? – Sneh

+0

@Sneh不幸的是,當時間設置改變時,遊戲被黑掉。如果遊戲離線,可以做些什麼? – Salihcan

回答

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(); 
} 
+1

Thx。現在它正在處理所有情況。 – Salihcan