I have this code which i got from an online tutorial to set up scores and highscores. the score works great but my problem is that it loads the same highscore for every level instead of just for the level on which the layer is on. since i have more than one layer(different lv in every layer) i placed it on all the layers rather than on a HUDlayer so it would belong to that one layer but it still doesnt individualise them. any help would be appreciated.
////LAYER1
- (void)setupHighScore{
// Get scores array stored in user defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Get high scores array from "defaults" object
NSArray *HighScores = [defaults arrayForKey:@"scores"];
// Create label to show player score
CGSize winSize = [CCDirector sharedDirector].winSize;
// Create a mutable string which will be used to store the score list
NSMutableString *scoresString = [NSMutableString stringWithString:@""];
// Iterate through array and print out high scores
for (int i = 0; i < [HighScores count]; i++)
{
[scoresString appendFormat:@"%i: %i\n", i + 2, [[HighScores objectAtIndex:i] intValue]];
}
CCLabelTTF *scoresLabel = [CCLabelTTF labelWithString:scoresString fontName:@"Courier" fontSize:16.0];
[scoresLabel setPosition:ccp(winSize.width * 0.9, winSize.height *0.95)];
[self addChild:scoresLabel z:5];
scorePoints = [CCLabelTTF labelWithString:@"HighScore " fontName:@"Courier" fontSize:16.0];
[scorePoints setPosition:ccp(winSize.width * 0.7555, winSize.height * 0.95)];
[self addChild:scorePoints z:5];
}
- (void)setupScore{
// Create label to show player score
CGSize winSize = [CCDirector sharedDirector].winSize;
score = [CCLabelTTF labelWithString:@"Lv2 Score" fontName:@"Courier" fontSize:16.0];
[score setPosition:ccp(winSize.width * 0.7555, winSize.height * 0.85)];
[self addChild:score z:5];
pointsDisplay = [CCLabelTTF labelWithString:@" 0" fontName:@"Courier" fontSize:16.0];
[pointsDisplay setPosition:ccp(winSize.width * 0.9, winSize.height * 0.85)];
[pointsDisplay setColor:ccc3(255, 255, 255)];
[self addChild:pointsDisplay z:5];
}
//////////ENDSCENE.MM
// Get high scores array from "defaults" object
NSMutableArray *HighScores = [NSMutableArray arrayWithArray:[defaults arrayForKey:@"scores"]];
// Iterate thru high scores; see if current point value is higher than any of the stored values
for (int i = 0; i < [HighScores count]; i++)
{
if (points >= [[HighScores objectAtIndex:i] intValue])
{
// Insert new high score
[HighScores insertObject:[NSNumber numberWithInt:points] atIndex:i];
// Remove last score
[HighScores removeLastObject];
// Re-save scores array to user defaults
[defaults setObject:HighScores forKey:@"scores"];
[defaults synchronize];
NSLog(@"Saved new high score of %i", points);
break;
}
}
}