简体   繁体   中英

displaying an array of high scores

I am still pretty new to objective-c and I am doing a finale project for my class. What i want to do is read from NSUserDefaults the array of 3 high scores and display them on the first screen.

Here is my code

 self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *highscores = [[NSArray alloc] initWithArray:[defaults arrayForKey:@"highscores"]];

    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",highscores(0),highscores(1),highscores(2); //error on this line
    myLabel.fontSize = 30;
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));

    [self addChild:myLabel];

when i try to pass the highscores i get a error that NSArrays are not functions or function points

Edit the line of error with this line,

myLabel.text = [NSString stringWithFormat:@"High Scores \n 1)%f \n 2)%f \n 3) %f",
[[highscores objectAtIndex:0] floatValue],
[[highscores objectAtIndex:1] floatValue],
[[highscores objectAtIndex:2] floatValue]]; 

replace line

myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",highscores(0),highscores(1),highscores(2);

with

myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",[highscores objectAtIndex:0],[highscores objectAtIndex:1],[highscores objectAtIndex:2];

Hope this will solve the problem.

Try bellow code .......
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *highscores = [[NSArray alloc] initWithArray:[defaults arrayForKey:@"highscores"]];

highscores = [highscores sortedArrayUsingComparator:
                            ^NSComparisonResult(id obj1, id obj2) {
                                if ([obj1 intValue] < [obj2 intValue]) {
                                    return NSOrderedAscending;
                                } else if ([obj1 intValue] > [obj2 intValue]) {
                                    return NSOrderedDescending;
                                } else {
                                    return NSOrderedSame;
                                }
                            }];
    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    myLabel.text = @"High Scores \n 1)%f \n 2)%f \n 3) %f",highscores(0),highscores(1),highscores(2); //error on this line
    myLabel.fontSize = 30;
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));

    [self addChild:myLabel];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM