Code Monkey home page Code Monkey logo

Comments (3)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 15, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 15, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 15, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.