2

そのため、最近、Libgdx のシリアライゼーション ツールに苦労しています。現在、プレイヤー クラスと GameEntry クラスの両方を、後でユーザーがアプリケーションを終了したときにアクセスできるファイルに書き込もうとしています。私の現在の方法は、私のアプリのコンピューター バージョンではうまく機能しますが、Android プラットフォームに関しては、同じ成功はありませんでした。マニフェストに追加しましたが、まだ次のエラーが発生します。

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

ヘルプやコメントをいただければ幸いです。

4

2 に答える 2

1

必要な文字列はプラットフォームごとに異なるため、絶対 (完全修飾) パスを使用したくない場合があります。Gdx.files.local()おそらくタイプ pathを使用したいと思うでしょう。

詳細については、 https://code.google.com/p/libgdx/wiki/FileHandlingを参照してください。Java/Android/Desktop/GWT/iOS ストレージ オプションの交差が複雑なため、Libgdx パスの選択は少し複雑です。

于 2013-07-24T07:49:34.110 に答える