I am currently making a 2d game but there are some problems with saving and loading. Here is my saving methgod which works perfectly:
public static void PLAYER_AND_WORLD_DATA_SAVE() {
FileWriter writer = null;
for (int a = 0; a < 30; a++) {
try {
writer = new FileWriter("saves/PlayerAndWorld.sav");
writer.write(Integer.toString(Game.world.x));
writer.write(System.getProperty( "line.separator" ));
writer.write(Integer.toString(Game.world.y));
writer.write(System.getProperty( "line.separator" ));
writer.write(Integer.toString(Game.world.type));
writer.write(System.getProperty( "line.separator" ));
writer.write(Integer.toString(Environment.hours));
writer.write(System.getProperty( "line.separator" ));
writer.write(Integer.toString(Environment.minutes));
writer.write(System.getProperty( "line.separator" ));
writer.write(Player.name);
writer.write(System.getProperty( "line.separator" ));
writer.write(Integer.toString(Player.money));
writer.write(System.getProperty( "line.separator" ));
writer.close();
writer = null;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
if (writer != null) { try { writer.close(); } catch (Exception e) {} }
}
However the following loading code doesnt work and hightlights underlines the words…(int)scanner.nextLine();
public void PLAYER_AND_WORLD_DATA_LOAD() {
Scanner scanner = new Scanner("saves/PlayerAndWorld.sav");
while (scanner.hasNextLine()) {
Game.world.x = (int)scanner.nextLine();
}
scanner.close();
}
The world.x is infact an integer and im tryting to convert the strings within the text document into an int so i can set the world.x to this number