且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在libGDX中保存带有首选项的数据

更新时间:2023-02-02 20:48:08

您所需要做的就是将关卡保存在某个集合中(例如Array<>),然后将该集合保存到首选项"中.您将需要将此集合转换为String(没有 putArray()函数或类似的东西),***的办法是将其JSON化.

all you need is to keep your levels in some collection (like Array<>) and then save this collection to Preferences. You will need to cast this collection to String (there's no putArray() function or something like this) and the best idea is to jsonify it.

JSON 是一种文件格式(类似于xml,但更轻巧),并在libgdx方面提供了良好的支持.实现目标的代码如下:

JSON is a file format (something like xml but much lighter) with good support from libgdx side. The code to achieve your goal is something like:

    FloatArray levels = new FloatArray();
    levels.add(5993);
    levels.add(5995);

    ...

    Preferences p = Gdx.app.getPreferences("SETTINGS");

    Json json = new Json();

    String levelsJson = json.toJson(FloatArray.class, levels);

    p.putString("levels", levelsJson);

现在您已经保存了关卡收藏,而要做的所有事情就是:

now you've got you levels collection saved and all you have to do to get it back is:

    FloatArray levels = json.fromJson(FloatArray.class, p.getString("levels");

关于, Michał