2013-07-24 39 views
2

最近我一直在努力使用Libgdx的序列化工具。我目前正試圖將我的播放器類和我的GameEntry類編寫到一個文件中,以便稍後在用戶退出應用程序時訪問該文件。我目前的方法在我的應用程序的計算機版本上運行的很好,但是當涉及到android平臺時,它沒有取得同樣的成功。我已將 添加到我的清單中,但我仍然收到以下錯誤。Libgdx - 序列化平臺問題

com.badlogic.gdx.utils.GdxRuntimeException: Error writing file: player.dat (Absolute) 

然後發生以下錯誤,導致遊戲崩潰。

com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: scores.dat (Absolute) 

這是這部分遊戲的代碼。我調用create()方法中的save和read方法,具體取決於平臺是否檢測到這些文件。

public static void saveFile(ArrayList<GameEntry> scores) throws IOException{   
    FileHandle file = Gdx.files.absolute("scores.dat"); 
    OutputStream out = null; 
    try{ 
     file.writeBytes((serialize(scores)), false); 
    }catch(Exception ex){ 

    }finally{ 
     if(out != null) try{out.close();} catch(Exception ex){} 
    } 

    Gdx.app.log(Asteroids.LOG, "Saving File: " + scores.toString()); 
} 

public static void savePlayer(Player player) throws IOException{   
    FileHandle file = Gdx.files.absolute("player.dat"); 
    PlayerData playerData = new PlayerData(player); 
    OutputStream out = null; 
    try{ 
     file.writeBytes((serialize(playerData)), false); 
    }catch(Exception ex){ 
     Gdx.app.log(Asteroids.LOG, ex.toString()); 
    }finally{ 
     if(out != null) try{out.close();} catch(Exception ex){} 
    } 

    Gdx.app.log(Asteroids.LOG, "Saving Player"); 
} 

@SuppressWarnings("unchecked") 
public static ArrayList<GameEntry> readFile() throws IOException, ClassNotFoundException{ 
    FileHandle file = Gdx.files.absolute("scores.dat"); 
    scores = (ArrayList<GameEntry>) deserialize(file.readBytes()); 

    Gdx.app.log(Asteroids.LOG, "Reading File: " + scores.toString()); 
    return scores; 
} 

public static Player readPlayer() throws IOException, ClassNotFoundException{ 
    Player player = null; 
    PlayerData playerData = null; 
    FileHandle file = Gdx.files.absolute("player.dat"); 
    playerData = (PlayerData) deserialize(file.readBytes()); 

    player = new Player(new Texture(Gdx.files.internal("Ships/defaultShip.png")), new Vector2((Gdx.graphics.getWidth()/2) - 25, 50), Player.PLAYER_WIDTH,Player.PLAYER_HEIGHT); 
    player.setHealth((int) playerData.getPlayerHealth()); 
    player.setMaxHealth((int) playerData.getPlayerMaxHealth()); 
    player.setShieldHealth(playerData.getPlayerShieldHealth()); 
    player.setBulletCount(playerData.getBulletCount()); 
    player.setShieldRegenRate(playerData.getPlayerShieldRegenRate()); 
    player.setPlayerScore(playerData.getPlayerScore()); 
    player.setEntityTexture(new Texture(Gdx.files.internal(playerData.getPlayerShipName()))); 
    player.update(); 

    Gdx.app.log(Asteroids.LOG, "Reading Player"); 
    return player; 
} 

public static byte[] serialize(Object obj) throws IOException { 
    ByteArrayOutputStream b = new ByteArrayOutputStream(); 
    ObjectOutputStream o = new ObjectOutputStream(b); 
    o.writeObject(obj); 
    return b.toByteArray(); 
} 

public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { 
    ByteArrayInputStream b = new ByteArrayInputStream(bytes); 
    ObjectInputStream o = new ObjectInputStream(b); 
    return o.readObject(); 
} 

任何幫助或意見將不勝感激。

回答

1

你可能不會使用絕對路徑想要使用絕對(完全限定)路徑,因爲您需要的字符串在每個平臺上都不相同。您可能想要使用Gdx.files.local() type path

查看https://code.google.com/p/libgdx/wiki/FileHandling瞭解詳情。 Libgdx路徑選擇有點複雜,因爲Java/Android /桌面/ GWT/iOS存儲選項的交集非常複雜。

+0

當我將文件路徑更改爲本地時,該文件似乎並未創建,因爲當我調用Gdx.files.local(myfile).exists()時,它始終在android上返回false,因爲它在PC上完美工作精細。我沒有收到任何錯誤,但它只是重新創建文件,即使我已經嘗試將它保存在以前的實例中 – bhafenri

+1

這很奇怪。 'Gdx.files.local(myfile).getLocalStoragePath()'顯示什麼?此外,myfile名稱是否包含任何斜線(目錄分隔符)?不過,也許是時候提出一個新的/單獨的問題。 –

+0

當我調用Gdx.files.local(「player.dat」)時,我似乎沒有一個名爲.getlocalstoragepath()的方法,對於Gdx.files.local.getlocalstoragepath(),它確實返回「data/data/com.me .Asteroids/files /「如果有幫助 – bhafenri

1

如果你比你需要充分的路徑是這樣

FileHandle handle = Gdx.files.absolute("/some_dir/subdir/myfile.txt"); 

您也可以參考此鏈接任何其他參考

https://code.google.com/p/libgdx/wiki/FileModule

+0

如果我想在PC和Android版本中訪問它,那麼您會在哪裏推薦我保存該文件? – bhafenri