3

android cocos2dを使用してゲームプロジェクトのスコアを計算します。そのゲームでは、最初のレベルを終了した後、次のレベルに進むので、その最初のレベルのスコアを保存し、次のレベルのスコアも追加します。そのスコアをandroid-cocos2dに保存するにはどうすればよいですか。

4

4 に答える 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];

- - - - - - - - - 読む

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

ここからの元のコード

于 2012-11-09T11:14:46.543 に答える
1

ここで実際に求めているのは、アプリのデータを保存する方法のように聞こえますが、cocos2d は単なる 2D レンダリング/グラフィック エンジンであるため、技術的には cocos2d とは関係ありません。

データを格納するデータベースを提供するアプリ用の顧客コンテンツ プロバイダーを作成することもできますが、この方法は通常、他のアプリがアプリケーション データにアクセスできるようにする場合に最適です。より迅速で汚れたアプローチとして、SharedPreferencesアプローチを使用することもできます。

Androidの永続性については、ここでよく説明されています

于 2012-05-22T15:24:04.607 に答える
0

単純にスコアを処理するには、「static int myscore ;」のような静的変数を使用できます。それにいくつかのロジックを適用します。それ以外の場合は、スコアイベントを処理するためにsqliteを使用します....!!!!

于 2013-02-22T18:34:08.273 に答える