簡體   English   中英

使用BufferedImages序列化對象的ArrayList

[英]Serializing an ArrayList of Objects with BufferedImages

我正在嘗試通過序列化包含敵人ArrayList的游戲對象來保存游戲狀態。 ArrayList上的每個項目都是一個對象,並且每個對象都包含一個BufferedImage來表示敵人。 Java拋出一個錯誤,即BufferedImages無法序列化。 我發現的所有解決方案都只是創建一個新對象,並用除圖像之外的所有數據填充它,但是我不確定列表設置的可能性。

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();
    }
}

謝謝!

在評論中回答了這個問題,因此我將在此處重新發布以結束問題。

將您不想序列化的屬性標記為瞬態。 Vg,私有臨時BufferedImage AlienImage;。 – SJuan76

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM