Android的libgdx preferences不工作 [英] Android libgdx preferences not working
问题描述
可能是一个愚蠢的问题,但我有什么改变,使这个code的工作:
Probably a stupid question, but what do I have to change to make this code work:
package com.hobogames.WizardsDuel;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
public class GameData {
public static String version = "1.0.0";
public static String data[] = {"version"};
public static final int VERSION = 0;
public static Preferences prefs = Gdx.app.getPreferences("WizWars");
public static boolean played = (!prefs.get().isEmpty());
public static String getString(int key){
return prefs.getString(data[key]);
}
public static void storePrefs(){
prefs.putString(getString(VERSION),version);
prefs.flush();
}
}
特别是部分不工作是打球在Android设备上,每次都是假的。在桌面上就先玩后是真实的。
The part in particular that isn't working is that "played" is false every time on an android device. On desktop it's true after the first play.
推荐答案
我怀疑你要在Android和桌面上的表现有所不同的静态
状态咬伤。你肯定当你退出Android上的应用程序的VM退出?如果您快速重新启动在Android您的应用程序时,系统会重复使用相同的Dalvik虚拟机为新的活动。因为静态布尔
已初始化,初始化不重新运行。如果删除静态
所以这个东西得到重新运行时, GAMEDATA
它创建(presumably实例没有被存储在静态变量),你应该得到的更远。
I suspect you're getting bitten by the static
state behaving differently in Android and on your Desktop. Are you sure the VM exits when you exit the app on Android? If you restart your app quickly on Android, the system will reuse the same Dalvik VM for the new Activity. Since the static boolean
is already initialized, the initialization isn't re-run. If you remove the static
so this stuff gets re-run when GameData
is created (presumably instances of it are not being stored in static variables) you should get farther.
请参阅 http://bitiotic.com/博客/ 2013/05/23 / libgdx-和Android的应用程序生命周期/
这篇关于Android的libgdx preferences不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!