Comments (3)
Anyhow, i found the solution to the problem after debugging. so it turns out,
the line:
out.write(Boolean.toString(soundEnabled));
does not actually add a newline character to the end of the string. So in other
words, we are writing all the settings in one line, as a string, which becomes
a problem when trying to parse that back upon loading the settings file. in
your settings file, you get something like "true10080503010" instead of
"true\n100\n80\n50\n30\n10", which is the proper way to write out the file, in
order for your "readLine()" to work later on.
As a side note, i also would prefer the name of the settings file to be in a
directory instead of just named ".mrnom". For this one file, it's fine;
however, in order to scale the project (like say several settings files) it's
better to put the settings under something like "/MrNom/settings" on my SD
card. that way i know not to erase the files also.
i'm writing the fix for the code here (in your Settings.java file, replace the
function below):
{{{
public static void save(FileIO files){
BufferedWriter out = null;
try{
out = new BufferedWriter(new OutputStreamWriter(files.writeFile(".mrnom")));
out.write(Boolean.toString(soundEnabled) + "\n");
for(int i=0; i<5; i++) out.write(Integer.toString(highscores[i]) + "\n");
} catch(IOException e){}
finally {
try{
if(out!=null) out.close();
} catch (IOException e) {}
}
}
}}}
and notice that i changed the line from
out.write(Integer.toString(highscores[i]));
to
out.write(Integer.toString(highscores[i]) + "\n");
because you want to actually add a delimiter.
that should make the code work.
If you want to also save the settings file in "/MrNom/settings.txt" then, you
have to change ".mrnom" to "settings.txt" in the Settings.java file. ALSO, you
have to change your AndroidFileIO.java constructor to say something like
{{{
public AndroidFileIO(AssetManager assets, String folderName){
this.assets = assets;
this.externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath() + folderName;
File dir = new File(this.externalStoragePath);
if(!dir.mkdirs()) Log.e("AndroidFileIO", "could not create directory: " + externalStoragePath);
}
}}}
and that way, you end up making the directory. Now, you also have to change the
way you call this constructor in your AndroidGame.java (which is in your
framework implementation package) by changing
fileIO = new AndroidFileIO(getAssets());
to
fileIO = new AndroidFileIO(getAssets(), "/MrNom/");
and that should be all the fixes you need.
P.S. i must say - this is an AWESOME BOOK!!! i LOVE it!!! it explains things so
well, and thank you so much for publishing this book. you have certainly
inspired another kid to be a developer :)
Original comment by [email protected]
on 10 Sep 2011 at 7:00
from beginning-android-games.
As Mario points out in this GREAT Book :D its better to use StringBuilder to
concatenate strings and avoid using the + operator for performance reasons:
public static void save(FileIO files) {
BufferedWriter out = null;
StringBuilder builder = new StringBuilder();
try {
out = new BufferedWriter(new OutputStreamWriter(
files.writeFile(".mrnom")));
builder.append(Boolean.toString(soundEnabled));
builder.append("\n");
out.write(builder.toString());
for (int i = 0; i < 5; i++) {
builder.delete(0,builder.length());
builder.append(Integer.toString(highscores[i]));
builder.append("\n");
out.write(builder.toString());
}
} catch (IOException e) {
Log.e("IOException", e.getMessage());
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
Log.e("IOException", e.getMessage());
}
}
}
Original comment by [email protected]
on 25 Sep 2011 at 8:55
from beginning-android-games.
or better yet:
for (int i = 0; i < 5; i++) {
builder.append(Integer.toString(highscores[i]));
builder.append("\n");
}
out.write(builder.toString());
sry :P
Original comment by [email protected]
on 25 Sep 2011 at 9:05
from beginning-android-games.
Related Issues (20)
- Chapter 4, pg: 180, Kindle edition: "while (true) {thread.join()}" ? HOT 3
- Cannot download projects HOT 2
- [Chapter 6] Mr Nom screen is not loaded correctly on android 3.0 emulator HOT 1
- Patch for /trunk/ch12-droidinvaders/src/com/badlogic/androidgames/droidinvaders/SettingsScreen.java
- [Chapter 7] error in GLSurfaceViewTest.java source code
- Eclipse doesn't allow @Override on the method headers
- TouchEvents are not scaled properly in all cases (on devices with a soft "bottom bar")
- Patch for /trunk/ch04-android-basics/src/com/badlogic/androidgames/MultiTouchTest.java
- i spent money to buy Beginning Android 4 Games Development, i want to get source code,but i do not download 7-zip HOT 1
- [Chapter 3] Cannot import project
- Display type deprecated HOT 1
- Field deprecated HOT 2
- Game says unfortunately stopped. Please help. HOT 1
- width instead of height in drawRect in AndroidGraphics in framework.impl
- Texture class missing width and height members
- Possible incorect method call in GLSurfaceViewTest.java HOT 1
- Miscalculation when drawing rectangles with AndroidGraphics.drawRect() (ch06-mrnom)
- AndroidGame onCreate does not calculate scale factors correctly
- Super Jumper - updateGameOver - indexOutOfBoundsException
- Errors and Warnings after cleaning projects
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from beginning-android-games.