我使用Android cocos2d来计算游戏项目中的得分,在该游戏中,完成第一级后会进入下一级,因此我想存储第一级的得分,并添加下一级的得分。如何在Android cocos2d中存储这些得分。
我使用Android cocos2d来计算游戏项目中的得分,在该游戏中,完成第一级后会进入下一级,因此我想存储第一级的得分,并添加下一级的得分。如何在Android cocos2d中存储这些得分。
------------- 写入
// 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];
这里是来自此处的原始代码。
听起来你想问的是如何为应用程序存储数据,这与cocos2d技术上没有任何关系,因为它只是一个二维渲染/图形引擎。
你可以为你的应用程序创建自定义内容提供程序,它将为你提供一个数据库来存储你的数据,但是这种方法通常最适合当你希望其他应用程序能够访问你的应用程序数据时使用。 你也可以采用更快速而简单的SharedPreferences方法。
Android持久性在这里有很好的描述。
对于简单处理分数的情况:您可以使用静态变量,例如 "static int myscore;" 并对其应用一些逻辑,否则可以使用 SQLite 处理分数事件....!!!!
我猜想如果你正在使用Java的cocos2d进行Android端口,那么上面的Objective C答案对你可能没有立即的用处。以下是一些想法。 首先,简单的方法是创建一个数据库,您可以读取和写入分数数据。这将与任何Android应用程序相同。 有一个已开源并实现了这一点的cocos2d Android应用程序..
一个具体的例子可以在这里找到... https://github.com/chuvidi2003/GidiGames/blob/master/src/com/denvycom/gidigames/PuzzleLayer.javamDbHelper = 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)) ;
}