Game Development Reference
In-Depth Information
Listing 3-12. Highscores.h
#import<Foundation/Foundation.h>
#import "Score.h"
@interface Highscores : NSObject<NSCoding>{
NSMutableArray* theScores;
}
@property (nonatomic, retain) NSMutableArray* theScores;
-(id)initWithDefaults;
-(void)addScore:(Score*)newScore;
Highscores . As we can see, Highscores does in fact
NSCoding . We also see that it contains an NSMutableArray called theScores ,
Highscore with
Scores and one for adding a new Scores object. Listing 3-13 shows how this class is
Highscores.m
@synthesize theScores;
-(id)initWithDefaults{
self = [super init];
if (self != nil){
theScores = [NSMutableArray new];
for (int i = 0;i<10;i++){
[self addScore:[Score score:1 At:[NSDate date]]];
}
}
return self;
}
-(void)addScore:(Score*)newScore{
[theScores addObject:newScore];
[theScores sortUsingSelector:@selector(compare:)];
while ([theScores count] > 10){
[theScores removeObjectAtIndex:10];
}
}
- (void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:theScores forKey:@"theScores"];
}
Search WWH ::




Custom Search