我使用android cocos2d计算游戏项目中的分数,在完成第一级后进入下一个级别的游戏中,所以我想存储第一级分数并添加下一个级别的分数。我如何将分数存储在 android-cocos2d 中。
4 回答
使用 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];
来自这里的原始代码
听起来您实际上在这里要求的是如何为应用程序存储数据,这在技术上与 cocos2d 无关,因为它只是一个 2d 渲染/图形引擎。
您可以为您的应用程序创建一个客户内容提供程序,这将为您提供一个数据库来存储您的数据,但是这种方法通常最适合您希望其他应用程序能够访问您的应用程序数据时。对于更快速和肮脏的方法,您还可以使用SharedPreferences方法。
android持久性在这里描述得很好
简单地处理分数:您可以使用静态变量,如“static int myscore;” 并在其上应用一些逻辑,否则使用 sqlite 来处理 score 事件 ....!!!!
我猜如果您在 java 中使用 cocos2d 用于 android 端口,那么上述目标 C 中的答案可能不会立即对您有用。这里有一些想法。首先,简单的方法是创建一个数据库,您可以在其中读取和写入乐谱数据。这将与任何 android 应用程序相同。有一个开源的 cocos2d android 应用程序,它实现了这个..
可以在这里找到一个具体的例子...... 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中的数据库交互的更多信息
最好的。