如何在Android cocos2d中存储游戏分数

3

我使用Android cocos2d来计算游戏项目中的得分,在该游戏中,完成第一级后会进入下一级,因此我想存储第一级的得分,并添加下一级的得分。如何在Android cocos2d中存储这些得分。


你是在说将分数保存在设备中以供加载吗?[保存在文件中?] - Jimmar
4个回答

3
使用NSKeyedArchiver类进行存储,这里有一个例子:

------------- 写入

        // 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];

这里是来自此处的原始代码。


1

听起来你想问的是如何为应用程序存储数据,这与cocos2d技术上没有任何关系,因为它只是一个二维渲染/图形引擎。

你可以为你的应用程序创建自定义内容提供程序,它将为你提供一个数据库来存储你的数据,但是这种方法通常最适合当你希望其他应用程序能够访问你的应用程序数据时使用。 你也可以采用更快速而简单的SharedPreferences方法。

Android持久性在这里有很好的描述。


0

对于简单处理分数的情况:您可以使用静态变量,例如 "static int myscore;" 并对其应用一些逻辑,否则可以使用 SQLite 处理分数事件....!!!!


"http://developer.android.com/training/basics/data-storage/shared-preferences.html" - Akarsh M

0

我猜想如果你正在使用Java的cocos2d进行Android端口,那么上面的Objective C答案对你可能没有立即的用处。以下是一些想法。 首先,简单的方法是创建一个数据库,您可以读取和写入分数数据。这将与任何Android应用程序相同。 有一个已开源并实现了这一点的cocos2d Android应用程序..

http://denvycom.com/blog/step-by-step-guide-on-how-to-build-your-first-slider-puzzle-game-in-cocos2d-for-android-part-1/

一个具体的例子可以在这里找到... 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)) ;
  }

以上的代码从数据库中提取了一些“动作”数据。 类似的方法也可以用来将数据保存到数据库中。 请参阅以下链接,了解有关在Android中与数据库交互的更多信息 http://developer.android.com/training/basics/data-storage/databases.html 祝好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接