如何在Cocos2d的列表中存储高分

时间:2013-03-26 23:31:19

标签: ios6 cocos2d-iphone xcode4.5

现在我已经在应用程序商店中创建了我的游戏“激光防御者”,但我有一件事我想不通,如何将高分存储在游戏的列表中。我有一个标签可以计算被摧毁的敌舰数量,但是我如何将最好的这些分数存储在列表中呢?这是我更新enemiesShot标签的代码:

  • (void)update:(ccTime)dt {

    NSMutableArray * projectilesToDelete = [[NSMutableArray alloc] init]; for(CCSprite * _jejectiles中的弹丸){     CGRect projectileRect = CGRectMake(                                        projectile.position.x - (projectile.contentSize.width / 2),                                        projectile.position.y - (projectile.contentSize.height / 2),                                        projectile.contentSize.width,                                        projectile.contentSize.height);

    NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *target in _targets) {
        CGRect targetRect = CGRectMake(
                                       target.position.x - (target.contentSize.width/2),
                                       target.position.y - (target.contentSize.height/2),
                                       target.contentSize.width,
                                       target.contentSize.height);
    
        if (CGRectIntersectsRect(projectileRect, targetRect)) {
            [targetsToDelete addObject:target];
        }
    }
    
    for (CCSprite *target in targetsToDelete) {
        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];
        _enemiesShot++;
        [_enemiesShotLabel setString:[NSString stringWithFormat:@"%d",_enemiesShot]];
    
    
    
    }
    
    if (targetsToDelete.count > 0) {
        [projectilesToDelete addObject:projectile];
    }
    [targetsToDelete release];
    

    }

    for(CCSprite * projectiles in projectilesToDelete){     [_projectiles removeObject:projectile];     [self removeChild:projectile cleanup:YES]; } [projectilesToDelete release];

    }

1 个答案:

答案 0 :(得分:0)

试试这个:

NSUserDefaults的

保存

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs,
[prefs synchronize];

检索

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];


// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];

当应用程序正在运行时,只需将所有数据保存在某些NSArray / MutableArray中,当应用程序即将进入后台时,将“保存”部分放在“applicationDidEnterBackground”中。

当应用程序恢复时,从“UserDefaults”中取回所有数据(检索上面的代码)。将该代码放在“ApplicationDidEnterForeground”中。 重新启动应用程序时,使用“ViewDidLoad”检索代码。