- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 GameState 保存为 JSON 时遇到问题。
最初我将 GameState 保存为 JSON,如下所示:
Json json = new JSON();
String save = json.prettyPrint(state);
我加载它的方式如下:
json.fromJson(GameState.class, save);
但是,状态加载不正确,所以我进行了一些测试。我做了以下事情:
class Car
{
public int id;
public Color color;
public Speed speed;
public Car(int id, Color color, Speed speed)
{
this.color = color;
this.id = id;
this.speed = speed;
}
}
class Speed
{
enum ASpeed
{
FAST, MEDIUM, SLOW;
}
ASpeed speed;
public Speed()
{
speed = ASpeed.FAST;
}
}
并在创建
@Override
public void create()
{
Json json = new Json();
Car car = new Car(1, Color.RED, new Speed());
String carSave = json.prettyPrint(car);
System.out.println(carSave);
}
我得到的结果是这样的:
{
id:1
color:{
r:1
a:1
}
speed:{}
}
我明白为什么我的游戏无法正确加载;它没有正确序列化。我的 GameState 几乎是一个包含 3 个需要在游戏之间进行的数据数组的类。我有常规的 getter 和 setter 方法以及几十个实用程序方法来访问相关数据,如下所示:
//TODO: Possibly add an Array of Event instances to fire for each player when certain circumstances are met as well as a string-based flagging system that can be used in the events.
//TODO: Research threading API in libGdx and see about moving the game logic over to a seperate thread - since this class is used for game logic, consider moving this with it.
public class GameState
{
private Array<Player> players;
private Array<Planet> planets;
private Array<Fleet> fleets;
public GameState(Array<Player> players, Array<Planet> planets, Array<Fleet> fleets)
{
this.players = players;
this.planets = planets;
this.fleets = fleets;
}
public Fleet getFleet(int id)
{
if(id <= fleets.size)
{
return fleets.get(id);
}
return null;
}
public Planet getPlanet(int id)
{
if(id <= planets.size)
{
return planets.get(id);
}
return null;
}
public Player getPlayer(int id)
{
if(id <= players.size)
{
return players.get(id);
}
return null;
}
当然,这不是最好的方法,但我每个字段只有大约 100 个,并且不打算添加更多,所以这种方法很好地满足了我的目的。每个播放器都包含一个用于获取相关数据的Integer
值数组。一个例子;
public class Player
{
private final int id;
//libGdx Color used by ShapeRenderer to display planets in the right political color
private Color color;
//Things that the game logic demands be accessible from each Player instance
//TODO: Move these over to the libGdx IntegerArray when you have the time - this will avoid the boxing/unboxing penalty when accessing members.
Array<Integer> allies;
Array<Integer>desiredAllies;
Array<Integer> enemies:
Array<Integer> neutrals;
Array<Integer> planets;
Array<Integer> fleets;
public Player(int id, Color color)
{
this.id = id;
this.color = color;
allies = new Array<Integer>();
planets = new Array<Integer>();
fleets = new Array<Integer>();
neutrals = new Array<Integer>();
enemies = new Array<Integer>();
}
//Getters, Setters, and many, many utility methods.
}
Planet、Player 和 Fleet 类都具有相似的结构,甚至实现了相同的接口(interface):
public interface IndexedGameObject extends GameObject
{
int getID();
}
我的游戏设计和结构在连载之前都运行良好。我专门学习 Json 来保存我的游戏,我可能错过了一些东西。 为什么这个设计在序列化时不起作用?有什么办法可以让它发挥作用吗?如果没有,除了编写我自己的保存格式之外,还有什么可能的解决方案?我对 XML 缺乏经验,这行得通吗?
注释:
我愿意并且能够编写自己的保存格式,但是以一致的、人眼可读的方式这样做会增加大量的开销和维护,并且会浪费时间(因为它类似于编写一个非常小的、不完全函数式的语言(我需要做大量的一般测试以确保它正常工作),并且将精力花在开发和维护更直观的用户界面上。如果 JSON 无法满足我的目的,有什么可以吗?
我基本上必须重新制定我的整个时间表(是的,我保留了一份我需要做的事情以及何时应该做的 list ,例如;星期二 - 修复那个烦人的、几乎不可能在放学后重新创建按钮消失的错误,以及作业)。
理论上,我的 JSON 应该具有这样的结构;
GameState ->
fleets ->
fleet ->
owner: integer id of the player that owns this fleet.
id: the index of this fleet in the Array
locationID: integer id of the province this fleet is located in.
originalTransports: final integer used for reinforcing.
originalFighters: final integer used for reinforcing.
originalFrigates: final integer used for reinforcing.
originalDreadnaughts: final integer used for reinforcing.
transports: number of troop transports currently in the fleet.
fighters: the number of fighters currently in fleet.
frigates: the number of frigates in this fleet.
dreadnaughts: number of dreadnaughts in the fleet.
players ->
player ->
id: int representing the index of this player in the GameState
provinces: Integer Array representing the provinces owned by this player.
allies: Integer Array representing the index of the other Players allied to this one.
enemies: Integer Array representing the indices of the other players at war with this one.
neutrals: Integer Array representing the players whom this player is neutral to
desiredAllies: Integer Array representing the players who this player desires for allies - used mainly by the ai to determine who to send alliance offers to and who to accept them from.
fleets: Integer Array representing the indices of the fleets of this player
provinces ->
province ->
id: int representing the index of this province
owner: int id of the owning player in the GameState
name: String value. Text displayed on the screen when province selected.
isUnderSiege: boolean. Read the variable name.
colonies ->
colony ->
name: String value. Displayed in the menu when the city is selected from the province info screen that pops up when the province is selected.
controller: The index of the controlling Player in the GameState.
population: int number of people who reside here. Used to calculate things such as how many ships this planet provides, or how much tax it gives to the player.
fortLevel: double representing the amount of organized military resistance this colony will give an attacker before surrendering.
growthRate: the number of births occurring in this colony every 60 ticks.
玩家阵列和舰队正常工作。当我到达省份数组时,事情变得很奇怪;
{
planets:[
//There should be 300 provinces. Instead 300 of these
colonies: [ {},{},{},{},{},{},{} ]
]
}
我的相关字段类看起来像这样;
public class Planet implements IndexedGameObject, Modifier
{
private int id;
private Array<Colony> colonies;
public Planet(int id, Array<Colony> colonies)
{
this.id = id;
this.colonies = colonies;
}
//Utility methods + Getters/Setters
}
class Colony
{
private int parentProvinceID;
private ColonyType type;
public Colony(int parentProvinceID, ColonyType type)
{
this.parentProvinceID = parentProvinceID;
this.type = type;
}
//Some more irrelevant methods
}
enum ColonyType
{
ADMINISTRATIVE, MILITARY, POPULATION;
}
最佳答案
您的问题是由通用数组引起的。我认为 JSON 不可能序列化,例如 Array<Player>
。
解决方案是包装通用数组 - take a look at this thread或者在可能的情况下使用非通用数组(例如 FloatArray 而不是 Array)。
<小时/>另一方面,你的逻辑结构似乎是关系型的 - 也许使用一些 SQL 技术而不是将其保留在对象中并序列化/反序列化会更方便? Here you have some nutshell info
关于java - libGdx 中的 GameState 到 JSON 的序列化不起作用,我该如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403339/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!