我正在制作一个有关卡的游戏,我正在寻找一种比较 2 次的方法。第一次是玩家完成关卡所需的时间第二次是我选择的时间。如果第一次小于第二次,则获得一星,如果高于第二次,则不获得一星。正如您在下面的代码中所见,我已将 2 次存储在 NSDates 中。我使用 NSComparisonResult 来比较结果。我的问题是 NSComparisonResult Result1 = 4294967295 而不是 NSOrderedDescending 或类似的东西。
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSComparisonResult result1;
NSComparisonResult result2;
NSComparisonResult result3;
// Create a date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
NSDate *level1ThreeStarRequirement = [dateFormatter dateFromString:@"00:01.500"];
NSDate *level1TwoStarRequirement = [dateFormatter dateFromString:@"00:02.500"];
NSDate *level1OneStarRequirement = [dateFormatter dateFromString:@"00:02.500"];
result1 = [timerDate compare:level1ThreeStarRequirement]; // comparing two dates
result2 = [timerDate compare:level1TwoStarRequirement];
result3 = [timerDate compare:level1OneStarRequirement];
if (result1 == NSOrderedAscending){
self.star1.image = [UIImage imageNamed:@"star1.png"];
self.star2.image = [UIImage imageNamed:@"star2.png"];
self.star3.image = [UIImage imageNamed:@"star3.png"];
}
if (result2 == NSOrderedAscending){
self.star1.image = [UIImage imageNamed:@"star1.png"];
self.star2.image = [UIImage imageNamed:@"star2.png"];
self.star3.image = [UIImage imageNamed:@"noStar.png"];
}
if (result3 == NSOrderedDescending){
self.star1.image = [UIImage imageNamed:@"star1.png"];
self.star2.image = [UIImage imageNamed:@"noStar.png"];
self.star3.image = [UIImage imageNamed:@"noStar.png"];
}
谢谢马蒂斯