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

时间:2011-12-08 04:15:43

标签: cocos2d-android

我使用android cocos2d在游戏项目中计算得分,在完成第一级后它进入下一级别的那个游戏中,所以我想存储第一级得分并添加下一级别得分。我怎么能在android-cocos2d中存储这些分数。

4 个答案:

答案 0 :(得分: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];

-----------------阅读

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

来自HERE的原始代码

答案 1 :(得分:1)

听起来你在这里实际要求的是如何存储应用程序的数据,这在技术上与cocos2d无关,因为它只是一个2D渲染/图形引擎。

您可以为您的应用创建customer content provider,它会为您提供存储数据的数据库,但这种方法通常最适合您希望其他应用能够访问您的应用数据的情况。对于更快速和更脏的方法,您还可以使用SharedPreferences方法。

很好地描述了

android持久性here

答案 2 :(得分:0)

简单处理得分:你可以使用像“static int myscore”这样的静态变量并在其上应用一些逻辑,否则使用sqlite来处理得分事件.... !!!!

答案 3 :(得分:0)

我猜你是否在java中使用cocos2d作为android端口,上面在目标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

最佳。