0
votes

Right now I have already created my game "Laser Defender" and its in the app store, but I have one thing I can't figure out, how to store the high scores in a list from the game play. I have a label that counts up the number of enemy ships that are destroyed, but how do I store the best of these scores in a list? Here is my code that updates the enemiesShot label:

  • (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.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 release];

    }

1
by "list" you mean "NSUSerDefaults" or you want only when the app is running ?Suny
I want to store all the highest scores in a list whether the app is running or not.Myers App Development

1 Answers

0
votes

try this :

NSUserDefaults

Saving

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

Retrieving

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

While the app is running just keep all the data in some NSArray/MutableArray and when the app is about to go in the background so the "Saving" part inside the "applicationDidEnterBackground".

When the app is resumed take all the data back from "UserDefaults" (retrieving code above).Place that code inside "ApplicationDidEnterForeground". When the application is started afresh , use "ViewDidLoad" for retrieving the code.