【问题标题】:android- Libgdx serialization and deserializationandroid-Libgdx 序列化和反序列化
【发布时间】:2014-07-19 05:10:35
【问题描述】:

我有一个Player 类,它记录玩家位置和其他几个变量。此外,为了在游戏结束后记住这些状态,我尝试序列化和反序列化我的 player 对象。这个过程适用于我的 libGdx 项目的桌面版本,但是当我使用我的 android 设备运行它时它会失败(我的坐标没有保存)。此外,当我检查“桌面”文件夹时,我可以看到 "player.dat" 文件,但是在我的 android 文件夹中它不存在...起初我想这可能与我调试的方式有关,并将实际游戏保存在我的电话并运行它,但即使这样坐标也没有保存。

播放器类:

public class Player implements Serializable {

    private static final transient String defaultTexture = "data/libgdx.png";

    private static final long serialVersionUID = 1L;
    transient Texture texture;
    Vector2 position;
    String textureLoc;
    transient Sprite sprite;

    public Player (Vector2 position, String textureLoc){
        this.position = position;
        this.textureLoc = textureLoc;
    }

    public void loadContent() {
        if (Gdx.files.internal(textureLoc).exists())
            texture = new Texture(Gdx.files.internal(textureLoc));
        else
            texture = new Texture(Gdx.files.internal(defaultTexture));
        sprite = new Sprite(texture);
    }

    public void update(OrthographicCamera camera) {

        if(Gdx.input.isTouched()){
            Vector3 tp = new Vector3();
            tp.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(tp);
            if(tp.y > sprite.getY()){
                position.y += 4f;
            }else{
                position.y -= 4f;
            }
        }           
    }

    public void draw(SpriteBatch batch) {
        batch.draw(texture, position.x, position.y);
    }

    public static void savePlayer(Player player) throws IOException {
        FileHandle file = Gdx.files.local("player.dat");
        try {
            file.writeBytes(serialize(player), false);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

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

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

    public Vector2 getPosition(){
        return position;
    }

    public void setPosition(Vector2 position){
        this.position = position;
    }
}

主类:

public class MyGdxGame extends ApplicationAdapter { 
    private OrthographicCamera camera;
    Player player;
    SpriteBatch batch;  
    Vector2 position = new Vector2(50,50);
    boolean x = false;
    boolean y = false;  

    BitmapFont font;    

    @Override
    public void create () {     
        font = new BitmapFont();
        font.setScale(50f);

        camera = new OrthographicCamera();
        batch = new SpriteBatch();
        player = new Player(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), "data/mario.jpg");
        if(Gdx.files.local("player.dat").exists()){
            System.out.println("Player Exists. Reading File ...");

            x = true;
            try {
                player = Player.readPlayer();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) { 
                e.printStackTrace();
            }           
        }else{
            System.out.println("Player does not exist. Creating new player ...");
            y = true;

            player = new Player(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), "data/mario.jpg");          
        }   

        player.loadContent(); 
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);   
        player.update(camera);
        batch.begin();
        if(x){font.draw(batch, "Player Exists. Reading File ...", 20, 20);}
        if(y){font.draw(batch, "Player does not exist. Creating new player ...", 100, 100);}
        player.draw(batch);
        batch.end();        
    }

    public void dispose(){
        try {
            Player.savePlayer(player);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我唯一的猜测是,在 android 上保存/获取序列化文件可能有一些细节,但即便如此,我还是遵循了 libGdx API,所以它应该可以工作。

【问题讨论】:

    标签: java android serialization libgdx


    【解决方案1】:

    万一以后其他人也有同样的问题,你需要做的就是将游戏状态保存在pausedispose 上。事实证明,序列化和反序列化过程一切正常,文件实际上存储在您的设备上就好了。主要问题是dispose 只有在手机手动杀死应用程序而不是你时才会被调用:) 所以就像我说的,在暂停时也保存游戏状态,也就是当你点击设备上的主页按钮时。例如:

     public void pause () { 
             try {
                Player.savePlayer(player);
             } catch (IOException e) {
                e.printStackTrace();
             }
     }
    

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 2012-03-17
      • 2011-02-22
      • 2016-04-03
      • 2013-11-11
      相关资源
      最近更新 更多