2014-11-24 39 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