2014-11-24 38 views
1

我试图通过序列化包含敌人ArrayList的游戏对象来保存游戏状态。 ArrayList上的每个项目都是一个对象,每个对象都包含一个BufferedImage来表示敌人。 Java抛出BufferedImages不可序列化的错误。我发现的所有解决方案都只是创建一个新对象,并用除图像之外的所有数据填充它,但我不确定设置列表的可能性如何。使用BufferedImages序列化对象的ArrayList

public void saveGame(){ 
    GamePanel game = new GamePanel(); 
    game.enemyList = enemyList; 
    game.player = new Player(this.getWidth(), this.getHeight()); 
    game.player.setScore(player.getScore()); 
    game.player.setPos(player.getX(), player.getY()); 

    try{ 
     FileOutputStream fileOut = new FileOutputStream("savegame.txt"); 
     ObjectOutputStream out = new ObjectOutputStream(fileOut); 
     out.writeObject(game); 
     out.close(); 
     fileOut.close(); 
    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
} 

谢谢!

+2

将您不想序列化的属性标记为“transient”。例如,'私有瞬态BufferedImage alienImage;'。 – SJuan76 2014-11-24 22:13:37

+0

完美的作品,非常感谢你! – cogm 2014-11-24 22:22:22

+0

这应该是一个答案 – ksmonkey123 2014-11-24 22:22:53

回答

0

这是在评论中回答的,所以我会在这里重新发布它来解决问题。

将您不想序列化的属性标记为瞬态。例如,私有瞬态BufferedImage alienImage ;. - SJuan76