簡體   English   中英

顯示高分

[英]displaying an array of high scores

我對Objective-C還是很陌生,我正在為班級做一個壓軸項目。 我想做的是從NSUserDefaults中讀取3個高分的數組,並將它們顯示在第一個屏幕上。

這是我的代碼

 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];

當我嘗試通過高分時,我得到一個錯誤,即NSArrays不是函數或函數點

用此行編輯錯誤行,

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]]; 

更換線

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

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

希望這能解決問題。

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM