我试图用Java序列化一个蛇游戏,其中游戏必须有“保存”和“加载”的选项。我没有收到任何错误,但每当我尝试打印出生命、时间等时。当生命和时间不应该为0时,它只会给我0。
这是我保存和加载部分的一些代码:
public void SaveGame() throws IOException {
PnlCentro pnlCentro = new PnlCentro();
FileOutputStream fileOut = new FileOutputStream(fileName);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(pnlCentro);
out.close();
}
public void LoadGame() throws FileNotFoundException, IOException, ClassNotFoundException {
PnlCentro p = null;
FileInputStream fileIn = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fileIn);
p = (PnlCentro) in.readObject();
System.out.println("Body: " + p.vecBody);
System.out.println("Life: " + p.life);
System.out.println("Timer: " + p.getTime());
in.close();
fileIn.close();
}