java - Libgdx - 序列化平台问题

标签 java android serialization io libgdx

最近我一直在努力使用 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/

相关文章:

Android非矩形按钮,如何处理?

Java:如何序列化生成的 Java 对象的第 3 方库?

node.js - NodeJS Passport session 序列化 - 将用户反序列化到 session 中?

ruby-on-rails - 如何编辑表单中的 Rails 序列化字段?

java - 由于同步顺序,以下Java程序是否必须打印 "num:1 m_i:2 "

java - Java 游戏中的 JLabel/JFrame 中不显示背景图像

java - 从 DialogFragment 调用 DialogFragment,getActivity() 中的 ClassCastException

android - 如何在不知道当前 Activity 的情况下在 Android 应用程序中显示对话框

java - 使用 Mockito NotaMockException 进行 Junit 测试

java - 如何使 Android 猜谜游戏将猜测次数限制为 3 次尝试,之后将显示一条消息