help with high scores

Forums Programming cocos2d support (graphics engine) help with high scores

This topic contains 3 replies, has 3 voices, and was last updated by  rrdz473 2 years, 7 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
Author Posts
Author Posts
September 16, 2011 at 6:04 pm #235307

rickyrod
@rickyrod

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: %in", 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;

}

}

}

September 16, 2011 at 9:08 pm #347792

Normal Force
@normal-force

Could you put the code between the code tags? It makes it so much easier to read.

From the look of things, all your scores are stored in an array called “scores” but it doesn’t look like you are differentiating between what level those scores were earned in. But like I said, the code is somewhat hard to read.

September 16, 2011 at 9:22 pm #347793

rickyrod
@rickyrod

thanx normal force. thats pretty much all the code. the only other thing i have is the points being distributed amongst the enemies. and this on the header:

int points;

CCLabelTTF *pointsDisplay;

CCLabelTTF *highScore;

CCLabelTTF *score;

CCLabelTTF *scorePoints;

i got the code from here:

http://ganbarugames.com/2011/03/cocos2d-game-tutorial-multitouch-asteroids-part-3/

EDIT: I also have this on the menuscene

` NSDictionary *defaultDefaults = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:0],

[NSNumber numberWithInt:0],

[NSNumber numberWithInt:0],

[NSNumber numberWithInt:0],

[NSNumber numberWithInt:0],

nil]

forKey:@”scores”];

[defaults registerDefaults:defaultDefaults];

[defaults synchronize];

`

}

September 17, 2011 at 12:23 am #347794

rrdz473
@rrdz473

why dont you try to put it on a .plist

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.