我有一些JSON文件的一般概念和寫入他們的問題。目前,我有一個我的遊戲項目的ObjectMap,問題是改變這張地圖。我有如下的一類叫做項目,更改ObjectMap以存儲在json Libgdx
public static class Item {
public String name;
public String type;
public String rarity;
public int reqLevel;
int priceBuy;
int priceSell;
int amount = 0;
public item() {}
public void setAmount(int amnt) {amount = amnt;}
}
所以我在遊戲中的所有這些項目的JSON文件,我可以在完全沒有加載它們。我想要做的是使用setAmount或任何其他方式更改一個項目的數量,目前所有項目都加載到ObjectMap中。我發佈在我的save()和load()之下。我認爲在寫回地圖之前我需要更改地圖內的項目,但我正在努力完成任何事情。
public void save()
{
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
file.writeString(json.prettyPrint(items), false);
}
public void load() {
Json json = new Json();
items = json.fromJson(ObjectMap.class, file);
}
編輯:@Sneh 其實我覺得完全可以接受做一個ObjectMap.class的fromJson,這是何等的JSON文件的樣子,更是讓你可以有多個項目等
*可能是錯誤的,但它在我故意創建的場景中加載和寫入完美,我遇到的問題是改變了特定項目數量變量!
//Test file with small amount of items
{
"SwordOfDeath": {
"class": "com.johnny.gamerpg.InventoryController$Sword",
"name": "Sword of Death",
"type" : "Sword",
"rarity": "Common",
"reqLevel": 1,
"proficiency": "Strength",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"DaggerOfDeath": {
"class": "com.johnny.gamerpg.InventoryController$Dagger",
"name": "Dagger of Death",
"type": "Dagger",
"rarity": "Rare",
"reqLevel": 3,
"proficiency": "Dexterity",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"DaggerOfLife": {
"class": "com.johnny.gamerpg.InventoryController$Dagger",
"name": "Dagger of Life",
"type": "Dagger",
"rarity": "Epic",
"reqLevel": 5,
"proficiency": "Dexterity",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"LifeSword": {
"class": "com.johnny.gamerpg.InventoryController$Sword",
"name": "Life Sword",
"type": "Sword",
"rarity": "Legendary",
"reqLevel": 45,
"proficiency": "Strength",
"damageMax": 100,
"damageMin": 99,
"priceBuy": 1000,
"priceSell": 2000,
"bonusMagic": 0,
"bonusStrength": 5,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 10,
"amount": 2
}
}
你可以顯示你創建和更改項目對象的位置嗎? – Sneh