放置并获取Libgdx首选项的地图

时间:2015-04-11 23:05:48

标签: java dictionary libgdx preferences

嘿,我在从libgdx中的首选项文件中获取信息时遇到问题。我把一些地图放入一些coords,以便我可以定位一些东西,但是当我从偏好文件(获取整个文件,而不仅仅是我放入的地图)中获取地图时,我无法读取那些coords我投入。
我可以从地图上获取密钥没问题,但是当我尝试从地图访问值时,我得到运行时错误java.lang.String无法强制转换。我尝试了不同的值,如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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

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