【问题标题】:How to store high scores in a list in Cocos2dCocos2d如何将高分存储在列表中
【发布时间】:2013-03-26 23:31:19
【问题描述】:

现在我已经在应用商店中创建了我的游戏“激光防御者”及其应用程序,但我有一件事我不知道,如何将高分存储在游戏列表中。我有一个标签来计算被摧毁的敌舰数量,但是我如何将这些分数中最好的存储在一个列表中?这是我更新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), 弹丸.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 *projectile in projectilesToDelete) { [_projectiles removeObject:projectile]; [self removeChild:projectile cleanup:YES]; } [projectilesToDelete 发布];

    }

【问题讨论】:

  • “list”是指“NSUSerDefaults”,还是仅在应用程序运行时才需要?
  • 无论应用程序是否运行,我都想将所有最高分存储在一个列表中。

标签: ios6 cocos2d-iphone xcode4.5


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多