gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:23:15 28 4
gpt4 key购买 nike

我使用 android cocos2d 计算游戏项目的分数,在该游戏中完成第一级后进入下一级别,所以我想存储第一级分数并添加下一级分数。我如何将分数存储在 android-cocos2d 中。

最佳答案

使用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

关于cocos2d-android - 如何在android cocos2d中存储游戏分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com