[英]displaying an array of high scores
I am still pretty new to objective-c and I am doing a finale project for my class. 我对Objective-C还是很陌生,我正在为班级做一个压轴项目。 What i want to do is read from NSUserDefaults the array of 3 high scores and display them on the first screen. 我想做的是从NSUserDefaults中读取3个高分的数组,并将它们显示在第一个屏幕上。
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 当我尝试通过高分时,我得到一个错误,即NSArrays不是函数或函数点
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.