I’m developing a game in processing. It’s about hitting a ball with a bat so that the ball is thrown back. If you hit the ball, you get a point or the score increases by 1.
I want to add a shop to the main menu, where you can buy something with the points later.
Now the problem:
I can’t manage it that when I leave the game and restart that the points remain saved.
I already tried saveStrings() and loadStrings(), but it didn’t work.
Every time the score changes, the new score is in the text file and the old score is deleted. There is only one line.
I actually wanted it to be that the points list themselves one below the other and are added with loadStrings() when they are retrieved. Then I would have all the points.
I am slowly really despairing.
Any help is welcome.
This assumes there’s only one person using the computer.
(Otherwise you need to ask the player for his or her name and store the score with this name.)
my approach:
the file contains only one line
on startup of the sketch check if the file is there.
if so set the score from file: score = strList[0];
if not score = 0;
during sketch say score++; or whatever
on ending the sketch you need a routine that is executed before leaving. You can achieve this when you catch ESC in keyPressed() (or quit in a menu point) and do the saving there.
save the current score to the file (overwriting the old file when it’s there)