Reputation: 1503
I calculates the score in game project using android cocos2d, in that game after finishing the first level it goes to the next level, so i want to store that first level score and also add next level scores. how can i store that scores in android-cocos2d.
Upvotes: 3
Views: 687
Reputation: 351
I am guessing if you are using cocos2d for android port in java, this answer above in objective C might not be immediately useful to you. Here are some thought. First, the simple approach is to create a database which you can read and write score data to . This will be the same as any android app. There is a cocos2d android application thats been open sourced and which implements this ..
A specific example can be found here ... https://github.com/chuvidi2003/GidiGames/blob/master/src/com/denvycom/gidigames/PuzzleLayer.java
mDbHelper = new DbAdapter(appcontext);
mDbHelper.open();
String labelmoves;
Cursor ScoreCursor = mDbHelper.fetchPuzzleBest("puzzlemania", GidiGamesActivity.currentpuzzletype,String.valueOf(NUM_ROWS)); // mDbHelper.fetchBestScore("puzzlemania", "");
if(ScoreCursor.getCount() > 0){
ScoreCursor.moveToFirst();
labelmoves = ScoreCursor.getString(ScoreCursor.getColumnIndex(
DbAdapter.KEY_GAME_MOVES)) ;
}
The above simple extracts some "moves" data from a database. A similar approach can be used to also save data to a database . See link below to learn more about database interaction in android http://developer.android.com/training/basics/data-storage/databases.html
Best.
Upvotes: 0
Reputation: 1611
for a simply handle the score : you can use the static variable like "static int myscore ;" and apply some logic on it otherwise use the sqlite for handling the score event ....!!!!
Upvotes: 0
Reputation: 11930
Use NSKeyedArchiver class to store, here you have an example:
------------- WRITE
// get allowed save paths
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// string for the default path
NSString *documentsDirectory = [paths objectAtIndex:0];
// full path plus filename for save game
NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
// storage for game state data
NSMutableData *gameData = [NSMutableData data];
// keyed archiver
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];
// encode all variables we want to track
[encoder encodeObject:[player inventoryDict] forKey:@"playerInventoryDict"];
[encoder encodeObject:[player equippedDict] forKey:@"playerEquippedDict"];
[encoder encodeInt:[player initialActionPoints] forKey:@"playerInitialActionPoints"];
[encoder encodeInt:[player actionPoints] forKey:@"playerActionPoints"];
[encoder encodeInt:[player experiencePoints] forKey:@"playerExperiencePoints"];
[encoder encodeInt:[player xpNextLevel] forKey:@"playerXPNextLevel"];
[encoder encodeBool:[player isThinking] forKey:@"playerIsThinking"];
// finish, write and release the encoder
[encoder finishEncoding];
[gameData writeToFile:gameStatePath atomically:YES];
[encoder release];
----------------- READ
// get allowed save paths
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// string for the default path
NSString *documentsDirectory = [paths objectAtIndex:0];
// full path plus filename for save game
NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
// storage for game state data
NSMutableData *gameData = [NSMutableData data];
// start the decoder
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
// start with player data
PlayerEntity *player = [PlayerEntity player];
// variables from the PlayerEntity Class
player.inventoryDict = (NSDictionary *)[decoder decodeObjectForKey:@"playerInventoryDict"];
player.equippedDict = (NSDictionary *)[decoder decodeObjectForKey:@"playerEquippedDict"];
player.initialActionPoints = [decoder decodeIntForKey:@"playerInitialActionPoints"];
player.actionPoints = [decoder decodeIntForKey:@"playerActionPoints"];
player.experiencePoints = [decoder decodeIntForKey:@"playerExperiencePoints"];
player.xpNextLevel = [decoder decodeIntForKey:@"playerXPNextLevel"];
player.isThinking = [decoder decodeBoolForKey:@"playerIsThinking"];
// release the decoder
[decoder release];
Original code from HERE
Upvotes: 3
Reputation: 229
sounds like what you're actually asking for here is how to store data for an app, which is technically nothing to do with cocos2d, as that's just a 2d rendering/graphics engine.
you could either create a customer content provider for your app which will give you a database to store your data in, however this approach is generally best suited for when you want other apps to be able to access your applications data. You could also use, for a more quick and dirty approach, a SharedPreferences approach.
android persistence is described well here
Upvotes: 1