在基于文本的游戏中加载和保存文件(java)

时间:2015-04-01 20:22:16

标签: java

所以,我正在尝试将我的播放器数据保存到txt文件中,然后从游戏再次打开时加载文本中的数据,但是它不起作用,我在游戏中将其保存,但是当我关闭游戏并将其打开时,它不再保存,它拥有游戏中的所有默认数据,并且在文件中......

以下是游戏中的一些代码(很抱歉,这是在pastebin中,我想我可能太长了,不能将它粘贴到这里。)

我需要一些帮助,尝试从文本文档中加载它,并在游戏打开时,不将文本文档重置为默认设置。

4 个答案:

答案 0 :(得分:2)

问题出在Save.savePlayer()

在开始时,您声明saveInfo以保存游戏在该时间点保持的值。最初加载时,它们将保留默认值。

int[] saveInfo = { Game.hp, Game.level, Game.mana, Game.expTotal,
                    Game.goldTotal, Game.arrow, Game.shuriken, Game.bomb,
                    Game.hpPotion, Game.mpPotion, Game.potion, Game.items };

此处的变量:Game.hp=100Save.saveInfo[0]=100

然后在saveInfo的开头将所有游戏变量设置为Save.savePlayer()

                    Game.hp = saveInfo[0];
                    Game.level = saveInfo[1];
                    Game.mana = saveInfo[2];
                    Game.expTotal = saveInfo[3];
                    Game.goldTotal = saveInfo[4];
                    Game.arrow = saveInfo[5];
                    Game.shuriken = saveInfo[6];
                    Game.bomb = saveInfo[7];
                    Game.hpPotion = saveInfo[8];
                    Game.mpPotion = saveInfo[9];
                    Game.potion = saveInfo[10];
                    Game.items = saveInfo[11];

此处的变量:Game.hp=100Save.saveInfo[0]=100 它们不会更改,因为您只需将它们设置回默认值。

然后加载已保存的状态,但不对数据执行任何操作。您应该在加载后设置变量,以便将它们设置为新的saveInfo值而不是旧值。

for (int i = 0; i < saveInfo.length; i++) {
    saveInfo[i] = Integer.parseInt(inputReader.readLine());
}

答案 1 :(得分:1)

我认为这部分是问题...

private void readPlayer(String filePath) {
    File inputFile;
    BufferedReader inputReader;

    try {
        inputFile = new File(filePath);
        inputReader = new BufferedReader(new FileReader(inputFile));

        Game.hp = saveInfo[0];
        Game.level = saveInfo[1];
        Game.mana = saveInfo[2];
        Game.expTotal = saveInfo[3];
        Game.goldTotal = saveInfo[4];
        Game.arrow = saveInfo[5];
        Game.shuriken = saveInfo[6];
        Game.bomb = saveInfo[7];
        Game.hpPotion = saveInfo[8];
        Game.mpPotion = saveInfo[9];
        Game.potion = saveInfo[10];
        Game.items = saveInfo[11];

        for (int i = 0; i < saveInfo.length; i++) {
            saveInfo[i] = Integer.parseInt(inputReader.readLine());
        }

        inputReader.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

把这些行

for (int i = 0; i < saveInfo.length; i++) {
    saveInfo[i] = Integer.parseInt(inputReader.readLine());
}

Game.hp = saveInfo[0];
Game.level = saveInfo[1];
Game.mana = saveInfo[2];
Game.expTotal = saveInfo[3];
Game.goldTotal = saveInfo[4];
Game.arrow = saveInfo[5];
Game.shuriken = saveInfo[6];
Game.bomb = saveInfo[7];
Game.hpPotion = saveInfo[8];
Game.mpPotion = saveInfo[9];
Game.potion = saveInfo[10];
Game.items = saveInfo[11];

为了在设置游戏值之前读取文件...

答案 2 :(得分:0)

如果您不介意,那么使用xstream和apache fileutils是一个很好的解决方案,而且编码较少。我只是分享我的想法。

import org.apache.commons.io.FileUtils;
import com.thoughtworks.xstream.XStream;

//saving your data
XStream xstream=new XStream(new DOMDriver());
String xml = xstream.toXML(yourGambeObj);
FileUtils.writeStringToFile(new File("yourfilename", xml);

//reading your data
Game gameData=xstream.fromXML(new File("yourfilename"),Game.class);

//now you can use access your methods n attribute. no conversion as you did in serialization.

请下载并添加这些罐子。XStream&amp; Apache Commons IO

答案 3 :(得分:0)

所以,我正在摆弄一些东西,我做到了!所以,问题是,我会从保存数据加载它,然后写它,然后再次加载它,由于某种原因,它会做整个Save.java 2次,只是保存它,但我把它们所有作为公共静态空洞而不是调用整个构造函数,我通过个别方法通过它们需要做什么和什么来调用它们,这看起来效果很好!谢谢大家的帮助,它帮助了很多!