PDA

View Full Version : High Scores




danmwall
Apr 23, 2009, 03:56 PM
I'm struggling trying to figure out how to store a list of high scores. I was planning on putting them in an NSMutableArray and sorting them. Sounded easy enough. I'm new to all of this, so I have only ever used NSMutableArrays with one value per instance. Now, for each instance I want to store a Name (NSString) and a Score (int). I have searched around and it looks like I may need to use an NSMutableDictionary?? I looked at the sample code in PeriodicElements, but it just confused me.

Does anyone have any sample code where you're using an NSMutableArray or NSMutableDictionary to store high scores?



mccannmarc
Apr 23, 2009, 04:37 PM
Very very basic example but something like the following would do it



// create a mutable dictionary with capacity of 2 (this is just the initial value, NSMutableDictionaries resize when needed anyway)
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:2];

// create the mutable arrays - score1,2 & 3 for example would be NSNumbers and name1,2 & 3 would be NSStrings
NSMutableArray * playerScores = [NSMutableArray arrayWithObjects:score1, score2, score3, nil];
NSMutableArray * playerNames = [NSMutableArray arrayWithObjects:name1, name2, name3, nil];

// add the arrays to the dictionary, (the key always has to be a string)
[dict setObject:playerScores forKey:@"usr_scores"];
[dict setObject:playerNames forKey:@"usr_playernames"];



Just in case you didn't know, all of these objects will be flagged for autorelease. If you need to hold on to them and access them beyond the method they are in you will need to alloc and init them and retain and release when necessary as well as make them class members rather than local variables

mpatric
Apr 24, 2009, 10:03 AM
Very very basic example but something like the following would do it


I don't see any reason to create a dictionary with static keys like this here, and decoupling the names and scores is not a good idea.

@danmwall, you should create a custom class, say HiScore, which has fields for name, score, and whatever else you want to store per line on the high score table (like level reached, time, etc.)

Then put HiScore objects into your NSMutableArray. You could sort the array by score using the sortUsingSelector method, something like this:

[myArray sortUsingSelector:@selector(compareWith:)];

You will need to write the small method referenced by the selector here (I've called it compareWith) on your HiScore class that does the actual comparison with another HiScore. It would look something like this (I'm not on my mac, so excuse any errors):

- (NSComparisonResult) compareWith:(HiScore*)other {
if (self.score < other.score) {
return NSOrderedAscending;
} else if (self.score > other.score) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}


That's one way to do it.

mccannmarc
Apr 24, 2009, 11:12 AM
The way I said works a treat in my game but having said that I have to do it that way as I am interfacing with CocosLive and the helper functions take a dictionary as input. In fact why don't you just use CocosLive rather than do it all yourself? You get the added bonus of free online high score table hosting too