Android File Problem

Hi all,

Just a quick question, my game runs fine reading in the level’s collision data from a JSON file. This works perfectly on windows but when I try to run it on Android, the game crashes once the level has loaded. I don’t understand why it will work on one platform and not the other. I’m using jsoncpp.cpp to parse my data.

The game runs fine (level will load but no collisions) if I comment out the JSON collisions bit.

I’m opening the file like this:
ifstream infile; infile.open (CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(lvl.getCollisionFile()));

I’m getting my data like this:
const Json::Value vertices = shapes[i].get("vertices",0);

Is there a problem with the NDK? (I’m using NDK R8C)

Any general ideas?

I don’t think ifstream works on Android.
Try *CCFileUtils::sharedFileUtils() -> getFileData()* instead.

Ah thank you for the reply.

I’ve managed to change my ifstream stuff to this:

@ // Load the JSON document
const char* fullpath = NULL;
unsigned long filesize;
unsigned char* data;
fullpath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(lvl.getCollisionFile());
data = CCFileUtils::sharedFileUtils()->getFileData(fullpath, “r”, &filesize);

std::string str;
str.append(reinterpret_cast<const char*>(data));@

Which gets my json data and places it in a string :slight_smile:

However, I have this wierd bug. If I leave the code like it is above, it still works on windows but won’t work on Android. It won’t find the assets even though they are in the correct place in the directory i think (in proj.android->assets).

When I replace the lvl.getCollisionFile()

with

fullpath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("levels/env1/level10/env1Lvl10Col.json");

My program still crashes. Is there something I’m not including or setting up correctly with eclipse? Is there something I need to edit to include the assets with my apk?

Dean

fullPathFromRelativePath* will only return what you pass to it, so whatever you pass to it must be an absolute path.
In my case, I placed my XML files on
Resources/ipad/Data/* and Resources/iphone/Data/, so I had two copies of each file. However, it doesn’t work on Android. What I did was to move the Data folder right after the Resources folder (Resources/Data instead of the previous one) then it worked.

Try placing your JSON files in Resources (so it would read Resources/levels/env1/level10/envLvl10Col.json)

Hi Lance, I appreciate the help.

I tried what you suggested. It is finding the file now and not coming up with a prompt. But when it attempts to load the level the screen goes black and then my phone reverts back to the menu. I’m really not sure on how to fix this at all. It’s so confusing because it works on windows fine! :frowning: I’ve removed ifstream and used your alternative. I’m so confused!

Any ideas?

Thanks

Dean

I tried what you suggested. It is finding the file now and not coming up with a prompt.
This means that the asset is being found properly.

But when it attempts to load the level the screen goes black and then my phone reverts back to the menu.
This means that the JSON file is not being processed properly. I’m really not sure how to fix this as I have not use JSON files (yet) in my app. Try printing out the string object you put the JSON data into and see if there are any problems on the contents.

There was a whole load of jibberish when i displayed it to the screen and not what i wanted. Same happens in both android and windows. Weird :S when i debug the information is all there. I’m thinking of changing it to XML because i have more experience working with that. Our levels are loaded with an XML file (not written by me, fellow project worker on a break from this!). It’s just our collision tool outputs our collision data to a json file.

my json file is like this:

"coords":[[{"x":0.5994173288345337,"y":0.37881335616111755},{"x":0.6134173274040222,"y":0.3675633668899536},{"x":0.6289173364639282,"y":0.3598133623600006},{"x":0.623917281627655,"y":0.37281334400177},{"x":0.6189172863960266,"y":0.38281333446502686}...

would i have to do the XML file like this?

<dict> <key>coords</key> <dict> <key>x1</key> <float>0.5994173288345337</float> <key>y1</key> <float>0.37881335616111755</float> <key>x2</key> <float> 0.613.....</float> <key>y2</key> <float>0.367....</float> </dict> </dict>

Not really sure how to do this multiple xy values into the CCDictionary

Dean