从 Libgdx 首选项中放置和获取地图

Put and get a map from Libgdx preferences

嘿,我在从 libgdx 中的首选项文件中获取信息时遇到问题。我放了一张带有一些坐标的地图,这样我就可以定位一些东西,但是当我从首选项文件(它获取整个文件,而不仅仅是我放入的地图)中获取地图时,我无法读取坐标我投了。
我可以从地图中获取键没问题,但是当我尝试从地图中访问值时,出现运行时错误 java.lang.String cannot be cast。我尝试了不同的值,如 Vector2 和 Float[],但我得到了相同的错误

这是我的代码:

public class SetSettings {
private Actor actor;
private Actor hit;
private Sprite sprite;
private Sprite sprite2;
private Rectangle rect;
private boolean customHit = false;
private ShapeRenderer render = new ShapeRenderer();
Array<Actor> actors = GameScreen.buttons.stage.getActors();

public SetSettings() {
    setOriginal();
    setCustom();
    rect = new Rectangle();
}

public void setOriginal() {
    learnGame.ass.settings.get().clear();
    float height = Gdx.graphics.getHeight();
    float width = Gdx.graphics.getWidth();
    // ui settings
    java.util.Map<String, Coords> map = new HashMap<String, Coords>();
    map.put("hpBar", new Coords(width - (learnGame.ass.hpBar.getWidth() * 1.02f), height - (height * .076f)));
    map.put("hpBase", new Coords(learnGame.ass.hpBar.getX(), learnGame.ass.hpBar.getY()));

    for (Entry<String, Coords> key : map.entrySet())
        System.out.println(key.getValue().x); // works fine here

}

public void setCustom() {
    java.util.Map<String, ?> amap =  learnGame.ass.settings.get();
    for (Entry<String, ?> key : amap.entrySet())  {
        if (key.getValue() instanceof Coords) {
            Coords coords = (Coords) key.getValue();
            float x_value = coords.x;
            float y_value = coords.y;
            System.out.println("6" + key.getKey());
            System.out.println("" + x_value);// <-----------error here -- java.lang.String cannot be cast to anything..

    }

}

public class Coords {
    float x;
    float y;

    public Coords(float x, float y) {
        this.x = x;
        this.y = y;
    }
}

}

检查 API 文档 - libgdx 不支持 libgdx preferences 中的数组或集合或对象(字符串除外)。如果您打算完全使用 libgdx 格式 - 考虑将您的复杂结构展平为关联的字符串数组或原始类型。

preferences.putFloat("hpBar_x", 123f);
preferences.putFloat("hpBar_y", 456f);