gpt4 book ai didi

java - Libgdx - 序列化平台问题

转载 作者:行者123 更新时间:2023-11-30 09:19:43 25 4
gpt4 key购买 nike

所以最近我一直在与 Libgdx 的序列化工具作斗争。我目前正在尝试将我的播放器类和我的 GameEntry 类写入一个文件,以便稍后在用户退出应用程序时访问该文件。我目前的方法在我的应用程序的计算机版本上运行得很好,但是当涉及到 android 平台时,它并没有取得同样的成功。我已经添加了到我的 list ,但我仍然收到以下错误。

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

然后出现以下导致游戏崩溃的错误。

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

这是游戏这部分的代码。我根据平台是否检测到这些文件,在 create() 方法中调用保存和读取方法。

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

如有任何帮助或意见,我们将不胜感激。

最佳答案

您可能不想使用绝对(完全限定)路径,因为您需要的字符串在每个平台上都不同。您可能想使用 Gdx.files.local() type path .

参见 https://code.google.com/p/libgdx/wiki/FileHandling了解详情。 Libgdx 路径选择有点复杂,因为 Java/Android/Desktop/GWT/iOS 存储选项的交集很复杂。

关于java - Libgdx - 序列化平台问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17825909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com