Hello, I made a Little game with greenfoot and I programmed a save-file System, that the game 'remembers' where the Player was and that stuff. It works Pretty good, except one Thing: At this Point of time, the Actor respawns at the same Point in every Level (A new Level is a new world-class), and I wanted to make, that I can make him respawn at other Points of the Level (For example if there are 2 layers in a Level). There are some txt files in the game Folder, which save the stats of the Actor. There are 6, but only 2 of them matter, because These 2 are for the coordinates and the other 4 ones do what they should do. The 2 file-names are in a Folder named 'SOOS' and the names of them are '0xFF_ReX.txt' and '0yFF_ReY.txt'. Another important Information is, that Java io is used to write and read the files. I used These 3 functions and pasted them in every class that Needs to Change any game file:
Here's an Explanation how to use them: write("",""), read("")
The 3rd function called convertStr only converts a string to int, it is used, because if I save a number in the file, it is saved as a String, but to use it, I have this function.
I have 3 classes, that are involved in my Problem, the word Level1() (Level1() is an example, all Levels use the same respawn-system), the Actor updateFile and the Actor Teleport(). UpdateFile() is an invisible Actor and is used to Change the respawn-coordinates in the next Level. The file-update gets triggered, when this Actor is touched by the Player. Teleport() is used to load the next Level, when it's touched. But it isn't that important in the Problem, you just Need to know, that it uses the function Greenfoot.setWorld() to load the next Level. The world Name is set in the constructor of teleport().
Now I will Show you, how the updateFile class manipulates the 2 files. I will Show you the whole Code of this class:
Basically, it uses the function write() to save the x and y coords to the 2 files, when the Player touches this class. The new x and y coords are defined in the constructor.
Now, let me explain you, how the world adds the Player object at the specific coordinates:
addObject(mar, convertStr("SOOS/0xFF_ReX.txt"), convertStr("SOOS/0yFF_ReY.txt"));
It just uses this Code, which reads the 2 txt files and adds the Player class at These 2 coordinates. It is called in the world constructor and is used in every word class. mar is the variable for the Player. The Player class is called Marcel, so this is the variable in the world class: Marcel mar = new Marcel();. Just as simple as that.
Now you hopefully know everything you Need to know to help me fix my Problem. The Basic Problem is, that the updateFile class saves the Location perfectly, when the Player touches it, but when the Player uses teleport() to go to the next Level, it still respawns him at the old Location, not on the one, that was just saved. Another strange Thing, that happens, is that the world respawns the Player at the new Location, when I reset the game using the button. So the coords can just be used to respawn the Player after the game gets resetted, so I hope, there's anyone who can help me fix this Problem. If you have some other Questions, how exactly the save-System works, or if something is still not clear, feel free to ask me.
void write(String dir, String txt) { String fileName = dir; try { FileWriter fileWriter = new FileWriter(fileName); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(txt); bufferedWriter.close(); } catch(IOException ex) { System.out.println( "Error writing to file '" + fileName + "'"); } } String read(String dir) { String fileName = dir; String line = null; try { FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); while((line = bufferedReader.readLine()) != null) { return line; } bufferedReader.close(); } catch(FileNotFoundException ex) { System.out.println( "Unable to open file '" + fileName + "'"); } catch(IOException ex) { System.out.println( "Error reading file '" + fileName + "'"); } return line; } int convertStr(String dir){ String str = read(dir); int value = Integer.parseInt(str); return value; }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.io.*; /** * Write a description of class UpdateFile here. * * @author (your name) * @version (a version number or a date) */ public class UpdateFile extends Actor { int nx; int ny; String xs; String ys; float once; /** * Act - do whatever the UpdateFile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. this.getImage().clear(); if(collisionDetection(Marcel.class)&&once==0){ xs = "" + nx; ys = "" + ny; Greenfoot.delay(5); write("SOOS/0xFF_ReX.txt",xs); write("SOOS/0yFF_ReY.txt",ys); once++; } } public UpdateFile(int nextX, int nextY){ nx = nextX; ny = nextY; xs = "" + nx; ys = "" + ny; once = 0; } void write(String dir, String txt) { String fileName = dir; try { FileWriter fileWriter = new FileWriter(fileName); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(txt); bufferedWriter.close(); } catch(IOException ex) { System.out.println( "Error writing to file '" + fileName + "'"); } } String read(String dir) { String fileName = dir; String line = null; try { FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); while((line = bufferedReader.readLine()) != null) { return line; } bufferedReader.close(); } catch(FileNotFoundException ex) { System.out.println( "Unable to open file '" + fileName + "'"); } catch(IOException ex) { System.out.println( "Error reading file '" + fileName + "'"); } return line; } int convertStr(String dir){ String str = read(dir); int value = Integer.parseInt(str); return value; } boolean collisionDetection(java.lang.Class thing){ Actor stuff = getOneIntersectingObject(thing); return stuff != null; } }