Issue
I'm writing a simple board game for Android. Each level is defined by an integer matrix holding the information of each tile of the level (i.e. is empty, there is an item, the item type, and so on).
I think that the best way to store these information is to insert them into a resource file (.xml).
Two questions:
- Is this really the best way/good practice to do this?
If so, how can I store a matrix of integer inside a resource files? I found few things about this, only articles talking about arrays, with too much xml tag. I'd like something like this:
< matrix > < row >12, 0, 14, 15, 0< /row > < row >2, 0, 0, 0, 0< /row > < row >12, 0, 0, 14, 0< /row > < row >1, 0, 3, 10, 10< /row > < /matrix >
Solution
I would suggest to store the data in a json or xml file (whatever suits you) and keep it under assets folder. Then use following function to read the data and parse it.
public String readFileFromAssets(String fileName) {
String result=null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
StringBuilder returnString = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
returnString.append(line);
}
result = returnString.toString();
reader.close();
} catch (IOException e) {
LogHelper.LogE("ERROR", e.getMessage());
}
return result;
}
Answered By - Muhammad Usama Shabbir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.