0

现在我已经在应用商店中创建了我的游戏“Laser Defender”和它,但是我有一件事我不知道,如何将高分存储在游戏列表中。我有一个标签来计算被摧毁的敌舰数量,但我如何将这些分数中最好的存储在一个列表中?这是我更新enemyShot标签的代码:

  • (void)update:(ccTime)dt {

    NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init]; for (CCSprite *projectile in _projectiles) { CGRect projectileRect = CGRectMake( projectile.position.x - (projectile.contentSize.width/2), projectile.position.y - (projectile.contentSize.height/2), projectile.contentSize。宽度,射弹.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 *projectile in projectilesToDelete) { [_projectiles removeObject:projectile]; [self removeChild:projectile cleanup:YES]; } [projectilesToDelete 释放];

    }

4

1 回答 1

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”来检索代码。

于 2013-03-28T03:53:05.370 回答