簡體   English   中英

在Android中為游戲保存/加載文件

[英]Save/Load a file in android for a game

我已經在Android中創建了一個游戲。 我編寫了一個用於輸入/輸出的類,並在外部安裝了首選安裝位置。 我想提出一些基本問題。 首先,我使用的文件是.txt(我知道它不是保存數據的最佳方法,但我將其用於測試)。 奇怪的是,游戲結束后,它應該自動保存用戶的高分,但事實並非如此,因此當我關閉應用程序並重新啟動它時,高分已經消失了。 我還想了解保存設置/高分/硬幣等(希望安全)的首選文件類型。 最后,我使用沒有外部存儲設備的Nexus 5調試器調試了游戲(不過應將其存儲在本地)。 這是我的代碼,在此先感謝:)。

public class AndroidFileIO implements FileIO {
    Context context;
    AssetManager assets;
    String externalStoragePath;

    public AndroidFileIO(Context context) {
        this.context = context;
        this.assets = context.getAssets();
        this.externalStoragePath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + File.separator;
    }

    public InputStream readAsset(String fileName) throws IOException {
        return assets.open(fileName);
    }

    public InputStream readFile(String fileName) throws IOException {
        return new FileInputStream(externalStoragePath + fileName);
    }

    public OutputStream writeFile(String fileName) throws IOException {
        return new FileOutputStream(externalStoragePath + fileName);
    }

    public SharedPreferences getPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }
}

我的游戲課有這種方法

public FileIO getFileIO() {
    return fileIO;
}

這是我加載文件的方式

Settings.load(game.getFileIO());

最后確定設置類的保存/加載方法

public static void load(FileIO files) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
                files.readFile("mrnom.txt")));
        soundEnabled = Boolean.parseBoolean(in.readLine());
        for (int i = 0; i < 5; i++) {
            highscores[i] = Integer.parseInt(in.readLine());
        }
    } catch (IOException e) {
        // :( It's ok we have defaults
    } catch (NumberFormatException e) {
        // :/ It's ok, defaults save our day
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
    }
}

public static void save(FileIO files) {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(
                files.writeFile("mrnom.txt")));
        out.write(Boolean.toString(soundEnabled));
        for (int i = 0; i < 5; i++) {
            out.write(Integer.toString(highscores[i]));
        }

    } catch (IOException e) {
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
        }
    }
}

在這里保存稱為

private void updateGameOver(List<TouchEvent> touchEvents) {
    int len = touchEvents.size();
    for(int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if(event.type == TouchEvent.TOUCH_UP) {
            if(event.x >= 128 && event.x <= 192 &&
               event.y >= 200 && event.y <= 264) {
                if(Settings.soundEnabled)
                    Assets.click.play(1);
                //debug begin
                FileIO fileIO = game.getFileIO();
                Settings.save(fileIO);
                //debug end
                game.setScreen(new MainMenuScreen(game));
                return;
            }
        }
    }
}

當您將字符串寫入out引用時,問題出在save方法中。 您不是在每行保存一個值,而是稍后在load方法中讀取每行的值。 使用當前代碼,將以下內容保存在mrnom.txt文件中: true10203040而不是true\\n10\\n20\\n30\\n40

要解決此問題,一種方法是更改​​:

out.write(Boolean.toString(soundEnabled));

out.write(Boolean.toString(soundEnabled) + "\n");

out.write(Integer.toString(highscores[i]));

out.write(Integer.toString(highscores[i]) + "\n");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM