We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The code works on one of my games on Java mode. But on another game that I want to run only in Android mode does not work. The code I created is:
class Score {
PrintWriter writer;
String[] reader = loadStrings("scores.txt");
int data;
int data2;
int data3;
Score() {
data = 0;
data2 = 0;
data3 = 0;
}
Score(int receiver, int receiver2, int receiver3) {
data = receiver;
data2 = receiver2;
data3 = receiver3;
}
void write() {
int[] collection = {data, data2, data3};
writer = createWriter("scores.txt");
for (int i = 0; i < collection.length; i++)
writer.println(collection[i]);
writer.flush();
writer.close();
}
int read() {
int piece = 0;
piece = int(reader[0]);
return piece;
}
int read2() {
int piece2 = 0;
piece2 = int(reader[1]);
return piece2;
}
int read3() {
int piece3 = 0;
piece3 = int(reader[2]);
return piece3;
}
}// End Class
Answers
The method write() is accessed one line before the exit() of the game. The read() methods are accessed in the setup() of the game. There is a .txt file on the sketch folder named scores.txt and inside are 3 zeros in column. The code of saving data, decision on high scores and reset of the high scores are inside the main code (the one that calls the Score class).
@infsys: can you post the error code in console???
debug: Ignoring <#text> tag. The file "scores.txt" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable. FATAL EXCEPTION: Animation Thread Process: processing.test.flappydragon, PID: 3298 java.lang.NullPointerException: Attempt to read from null array at processing.test.flappydragon.flappyDragon$Score.read(flappyDragon.java:827) at processing.test.flappydragon.flappyDragon.setup(flappyDragon.java:159) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:818) The file "scores.txt" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
I think on android mode the .txt file is not readable. I try with other codes now erased using .dat files and even a .tsv I found reading. But I don't know how to work with the .tsv on my code. It contained a try and a catch and stuff I don't really know.
Maybe I have to place the .txt file inside a specific folder of the sketch folder?
@infsys:: error is that NullPointerException for your .txt. Where have you put it?
In the sketch folder. A copy-paste from one game to another. It works on Java mode and not on android. The file is not inside a folder on sketch folder. How can I fix NullPointerException?
I placed it in data folder inside the sketch folder and now the game runs. But the class does not work. I will keep checking.
The PrintWriter is not working. It seems that the file can be readed.
@infsys:: i dont know wether printWriter is supposed to work with android; but i am sure that java OutPutStream works (something like this::)
I do not know Java. So in write() method, where it is written, and now I add the type, PrintWriter writer = createWriter("scores.txt"); I replace that line with your code? Where does I write the "scores.txt" string?
@infsys:: you comment your write() method; --- you create another one (with the same name if you want...) --- in this method you put my code --- with this method it will create a file named "try.txt" where you have tell with the path before its name; in my code i have choosen the internal memory storage on the phone; you can change that but this is the most common choice. Of course you can change the name for the file! -- you have to import java.io BufferedInputStream && FileOutPutStream --- and probably some android stuff (Environment) ---you have to add permission for writing external storage to your manifest --- after my code in the same method you have to "write" to the outPutStream in order to add content to the created file; for that you use a for loop (as you have done in the initial method)
something like that::
Note that with my code it will create a new "try.txt"---- or "scores.txt" at each time you call the method; but if you want you can also ask for "file existing already???" before, and choose to append something to your file. If you want to get the created file you do the inverse : create fileInputStream (with the same path && file name) read the file and use what you want from it!
good luck!
Environment, BufferedOutputStream and FileNotFoundExceptions are not recognized by the PDE. So now is the time I export it to eclipse? Is the following code good?
I will stop the process of this game for now. I have a Circuit Analysis test tomorrow and I need to practice. Thank you for the help.
@infsys:: put your import statements: i guess they are not correct...
@infsys::: i have tested in processing, these imports work::
Thanks again.