创建高分榜单

时间:2012-05-01 16:21:30

标签: ios iphone ipad

我正在制作一个游戏应用程序,你试图获得尽可能多的积分,我一直在尝试创建一个高分榜单。我这样做是通过测量分数是否高于前一个数字,如果它然后将第一个数字设置为2,2更为3,3个等等,将所有内容向下推进一步你知道。但我正在使用的方法不起作用;(

显示得分代码: 代码:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"highscore.save"];

NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];

myDict = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSString *nssScore1 = [myDict objectForKey:@"Score1"];
NSString *nssName1 = [myDict objectForKey:@"Name1"];
NSString *nssScore2 = [myDict objectForKey:@"Score2"];
NSString *nssName2 = [myDict objectForKey:@"Name2"];
NSString *nssScore3 = [myDict objectForKey:@"Score3"];
NSString *nssName3 = [myDict objectForKey:@"Name3"];
NSString *nssScore4 = [myDict objectForKey:@"Score4"];
NSString *nssName4 = [myDict objectForKey:@"Name4"];
NSString *nssScore5 = [myDict objectForKey:@"Score5"];
NSString *nssName5 = [myDict objectForKey:@"Name5"];
NSString *nssScore6 = [myDict objectForKey:@"Score6"];
NSString *nssName6 = [myDict objectForKey:@"Name6"];
NSString *nssScore7 = [myDict objectForKey:@"Score7"];
NSString *nssName7 = [myDict objectForKey:@"Name7"];
NSString *nssScore8 = [myDict objectForKey:@"Score8"];
NSString *nssName8 = [myDict objectForKey:@"Name8"];
NSString *nssScore9 = [myDict objectForKey:@"Score9"];
NSString *nssName9 = [myDict objectForKey:@"Name9"];
NSString *nssScore10 = [myDict objectForKey:@"Score10"];
NSString *nssName10 = [myDict objectForKey:@"Name10"];
highestscore = [nssScore1 intValue];
score1i = [nssScore1 intValue];
score2i = [nssScore2 intValue];
score3i = [nssScore3 intValue];
score4i = [nssScore4 intValue];
score5i = [nssScore5 intValue];
score6i = [nssScore6 intValue];
score7i = [nssScore7 intValue];
score8i = [nssScore8 intValue];
score9i = [nssScore9 intValue];
score10i = [nssScore10 intValue];

if(score1i > 0){
    NSString *str1 = [NSString stringWithFormat:@"1. %@ | %i points", nssName1, score1i];
    score1.text = str1;
} else {
    score1.text = @"No Scores available yet";
}
if(score2i > 0){
    NSString *str2 = [NSString stringWithFormat:@"2. %@ | %i points", nssName2, score2i];
    score2.text = str2;
} else {
    [score2 setHidden:1];
}
if(score3i > 0){
    NSString *str3 = [NSString stringWithFormat:@"3. %@ | %i points", nssName3, score3i];
    score3.text = str3;
} else {
    [score3 setHidden:1];
}
if(score4i > 0){
    NSString *str4 = [NSString stringWithFormat:@"4. %@ | %i points", nssName4, score4i];
    score4.text = str4;
} else {
    [score4 setHidden:1];
}
if(score5i > 0){
    NSString *str5 = [NSString stringWithFormat:@"5. %@ | %i points", nssName5, score5i];
    score5.text = str5;
} else {
    [score5 setHidden:1];
}
if(score6i > 0){
    NSString *str6 = [NSString stringWithFormat:@"6. %@ | %i points", nssName6, score6i];
    score6.text = str6;
} else {
    [score6 setHidden:1];
}
if(score7i > 0){
    NSString *str7 = [NSString stringWithFormat:@"7. %@ | %i points", nssName7, score7i];
    score7.text = str7;
} else {
    [score7 setHidden:1];
}
if(score8i > 0){
    NSString *str8 = [NSString stringWithFormat:@"8. %@ | %i points", nssName8, score8i];
    score8.text = str8;
} else {
    [score8 setHidden:1];
}
if(score9i > 0){
    NSString *str9 = [NSString stringWithFormat:@"9. %@ | %i points", nssName9, score9i];
    score9.text = str9;
} else {
    [score9 setHidden:1];
}
if(score10i > 0){
    NSString *str10 = [NSString stringWithFormat:@"10. %@ | %i points", nssName10, score10i];
    score10.text = str10;
} else {
    [score10 setHidden:1];
}

并保存得分代码:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"highscore.save"];

NSMutableDictionary* myDict1 = [[NSMutableDictionary alloc] init];

myDict1 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSString *nssScore1 = [myDict1 objectForKey:@"Score1"];
NSString *nssName1 = [myDict1 objectForKey:@"Name1"];
NSString *nssScore2 = [myDict1 objectForKey:@"Score2"];
NSString *nssName2 = [myDict1 objectForKey:@"Name2"];
NSString *nssScore3 = [myDict1 objectForKey:@"Score3"];
NSString *nssName3 = [myDict1 objectForKey:@"Name3"];
NSString *nssScore4 = [myDict1 objectForKey:@"Score4"];
NSString *nssName4 = [myDict1 objectForKey:@"Name4"];
NSString *nssScore5 = [myDict1 objectForKey:@"Score5"];
NSString *nssName5 = [myDict1 objectForKey:@"Name5"];
NSString *nssScore6 = [myDict1 objectForKey:@"Score6"];
NSString *nssName6 = [myDict1 objectForKey:@"Name6"];
NSString *nssScore7 = [myDict1 objectForKey:@"Score7"];
NSString *nssName7 = [myDict1 objectForKey:@"Name7"];
NSString *nssScore8 = [myDict1 objectForKey:@"Score8"];
NSString *nssName8 = [myDict1 objectForKey:@"Name8"];
NSString *nssScore9 = [myDict1 objectForKey:@"Score9"];
NSString *nssName9 = [myDict1 objectForKey:@"Name9"];

self.String = textname.text;
NSString *nameString = String;
NSString *nssHighScore = [NSString stringWithFormat:@"%i", highestscore];
NSString *nssIdentifier = [NSString stringWithFormat:@"%@", nameString];

NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];

[myDict setObject:nssIdentifier forKey:@"Name1"];
[myDict setObject:nssHighScore forKey:@"Score1"];
[myDict setObject:nssName1 forKey:@"Name2"];
[myDict setObject:nssScore1 forKey:@"Score2"];
if(nssScore2 > 0){
[myDict setObject:nssName2 forKey:@"Name3"];
[myDict setObject:nssScore2 forKey:@"Score3"];
}
if(nssScore3 > 0){
[myDict setObject:nssName3 forKey:@"Name4"];
[myDict setObject:nssScore3 forKey:@"Score4"];
}
if(nssScore4 > 0){
[myDict setObject:nssName4 forKey:@"Name5"];
[myDict setObject:nssScore4 forKey:@"Score5"];
}
if(nssScore5 > 0){
[myDict setObject:nssName5 forKey:@"Name6"];
[myDict setObject:nssScore5 forKey:@"Score6"];
}
if(nssScore6 > 0){
[myDict setObject:nssName6 forKey:@"Name7"];
[myDict setObject:nssScore6 forKey:@"Score7"];
}
if(nssScore7 > 0){
[myDict setObject:nssName7 forKey:@"Name8"];
[myDict setObject:nssScore7 forKey:@"Score8"];
}
if(nssScore8 > 0){
[myDict setObject:nssName8 forKey:@"Name9"];
[myDict setObject:nssScore8 forKey:@"Score9"];
}
if(nssScore9 > 0){
[myDict setObject:nssName9 forKey:@"Name10"];
[myDict setObject:nssScore9 forKey:@"Score10"];
}

NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath1 = [paths1 objectAtIndex:0];
NSString *path1 = [documentPath1 stringByAppendingPathComponent:@"highscore.save"];

[NSKeyedArchiver archiveRootObject:myDict toFile:path1];

现在这样做,Highscore#1已被保存并替换旧的,但之前的第一个刚刚消失,而其他(3个,4个,5个等)都没有显示。你能找到错误或我错过的东西吗?谢谢!

1 个答案:

答案 0 :(得分:2)

首先是第一件事。你做错了!

使用数组创建一个高分管理器类!这是一个例子:

<强> HighScore.h

@interface HighScore : NSObject<NSCoding>

+(id) highScoreWithName:(NSString *) name score:(int) score;

-(id) initWithName:(NSString *) name score:(int) score;

@property (assign) int score;
@property (copy)   NSString *name;

@end

<强> HighScore.m

@implementation HighScore

@synthesize score = _score, name = _name;

+(id) highScoreWithName:(NSString *)name score:(int)score
{
    return [[self alloc] initWithName:name score:score];
}

-(id) initWithName:(NSString *)name score:(int)score
{
    if ((self = [super init]))
    {
        self.name = name;
        self.score = score;
    }

    return self;
}

-(id) initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super init]))
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.score = [aDecoder decodeIntForKey:@"score"];
    }

    return self;
}

-(void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInt:self.score forKey:@"score"];
}

-(NSString *) description
{
    return [NSString stringWithFormat:@"%@ - %@: %i", [super description], self.name, self.score];
}

@end

<强> HighScoreManager.h

@interface HighScoreManager : NSObject {
    NSMutableArray *_scores;
}

+(HighScoreManager *) instance;

-(void) addHighScore:(HighScore *) score;
-(void) clearHighScores;

-(HighScore *) bestScore;
-(NSArray *) sortedScores;

@end

<强> HighScoreManager.m

@implementation HighScoreManager

+(HighScoreManager *) instance
{
    static HighScoreManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HighScoreManager alloc] init];
    });

    return manager;
}

-(id) init
{
    if ((self = [super init]))
    {
        // create from the file
        _scores = [NSKeyedUnarchiver unarchiveObjectWithFile:@"highscore.sav"];

        if (_scores == nil)
            _scores = [NSMutableArray new];
    }

    return self;
}

-(void) _synchronize
{
    // write to the file
    [NSKeyedArchiver archiveRootObject:_scores toFile:@"highscore.sav"];
}

-(void) addHighScore:(HighScore *)score
{    
    // add the score to the end of the array
    [_scores addObject:score];

    // sort on insert
    [_scores sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        HighScore *hs1 = obj1;
        HighScore *hs2 = obj2;

        return hs1.score - hs2.score;
    }];

    //  then synchronize
    [self _synchronize];
}

-(void) clearHighScores
{
    // remove all of the objects
    [_scores removeAllObjects];

    // then synchronize
    [self _synchronize];
}

-(HighScore *) bestScore
{
    // simply return the last object in the array
    return [_scores lastObject];
}

-(NSArray *) sortedScores
{
    // note this returns them from lowest to highest scores.
    // also notice we make a copy of the array. This way we 
    // can still add scores without compromising the reciever 
    // of this function.
    return [NSArray arrayWithArray:_scores]; 
}

@end

然后,您只需添加到高分列表中,它就会为您处理所有内部内容。