我正在Xcode中制作一个简单的游戏,我将NSTimers用于不同的目的,例如得分计时器,倒数计时器,以及移动我的对象。按下按钮时会调用所有这些计时器。但问题是得分计时器使所有其他计时器重启,它可以在一秒钟后,半秒钟,这取决于得分计时器的时间间隔。我知道这一点,因为我已经删除了分数计时器,然后一切正常。
而且,我正在使用NSInteger将分数存储为高分。
我似乎无法弄清楚为什么会这样。有什么想法吗?
我的代码:
- (IBAction)Start:(id)sender {
FuelMovement = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(FuelMoving) userInfo:nil repeats:YES];
FuelMovement2 = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(Fuel2Moving) userInfo:nil repeats:YES];
RoyMovement = [NSTimer scheduledTimerWithTimeInterval:0.0035 target:self selector:@selector(RoyMoving) userInfo:nil repeats:YES];
ObstacleTimer = [NSTimer scheduledTimerWithTimeInterval:0.0055 target:self selector:@selector(ObstaclesMoving) userInfo:nil repeats:YES];
CountDownTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(TimerCountDown) userInfo:nil repeats:YES];
Scorer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(Scoring) userInfo:nil repeats:YES];
- (void)Scoring {
ScoreNumber = ScoreNumber + 1;
ScoreLabel.text = [NSString stringWithFormat:@"%i", ScoreNumber];
if (ScoreNumber == 500) {
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:40]];
[Pling play];
[ObstacleTimer invalidate];
ObstacleTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.0045 target:self selector:@selector(ObstaclesMoving) userInfo:nil repeats:YES];
[RoyMovement invalidate];
RoyMovement2 = [NSTimer scheduledTimerWithTimeInterval:0.0025 target:self selector:@selector(RoyMoving) userInfo:nil repeats:YES];
}
if (ScoreNumber == 502) {
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:35]];
}
if (ScoreNumber == 1000) {
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:40]];
[Pling play];
[ObstacleTimer2 invalidate];
ObstacleTimer3 = [NSTimer scheduledTimerWithTimeInterval:0.0040 target:self selector:@selector(ObstaclesMoving) userInfo:nil repeats:YES];
[RoyMovement2 invalidate];
RoyMovement3 = [NSTimer scheduledTimerWithTimeInterval:0.0020 target:self selector:@selector(RoyMoving) userInfo:nil repeats:YES];
}
if (ScoreNumber == 1002) {
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:35]];
}
if (ScoreNumber == 5000) {
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:40]];
[Thousand play];
}
if (ScoreNumber == 5002) {
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:35]];
}
}
- (void)GameOver {
[Scorer invalidate];
[CountDownTimer invalidate];
[RoyMovement invalidate];
[RoyMovement2 invalidate];
[RoyMovement3 invalidate];
[ObstacleTimer invalidate];
[ObstacleTimer2 invalidate];
[ObstacleTimer3 invalidate];
[FuelMovement invalidate];
[FuelMovement2 invalidate];
ScoreLabel.frame = CGRectMake(331, 80, 140, 90);
[ScoreLabel setFont:[UIFont fontWithName:@"04b_21" size:30]];
if (ScoreNumber > HighScoreNumber) {
[[NSUserDefaults standardUserDefaults] setInteger:ScoreNumber forKey:@"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:@"%i", ScoreNumber];
NewHighScore.hidden = NO;
}
if (ScoreNumber > 99) {
Bronze.hidden = NO;
Silver.hidden = YES;
Gold.hidden = YES;
Diamond.hidden = YES;
Bronze.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Bronze.png"],
[UIImage imageNamed:@"Bronze2.png"],
[UIImage imageNamed:@"Bronze3.png"],
[UIImage imageNamed:@"Bronze4.png"],
[UIImage imageNamed:@"Bronze3.png"],
[UIImage imageNamed:@"Bronze2.png"],
[UIImage imageNamed:@"Bronze.png"],
[UIImage imageNamed:@"Bronze5.png"],
[UIImage imageNamed:@"Bronze6.png"],
[UIImage imageNamed:@"Bronze7.png"],
[UIImage imageNamed:@"Bronze6.png"],
[UIImage imageNamed:@"Bronze5.png"],
[UIImage imageNamed:@"Bronze.png"],
[UIImage imageNamed:@"Bronze8.png"],
[UIImage imageNamed:@"Bronze9.png"],
[UIImage imageNamed:@"Bronze10.png"],
[UIImage imageNamed:@"Bronze9.png"],
[UIImage imageNamed:@"Bronze8.png"],
nil];
[Bronze setAnimationRepeatCount:0];
Bronze.animationDuration = 1.2;
[Bronze startAnimating];
}
if (ScoreNumber > 499) {
Bronze.hidden = YES;
Silver.hidden = NO;
Gold.hidden = YES;
Diamond.hidden = YES;
Silver.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Silver.png"],
[UIImage imageNamed:@"Silver2.png"],
[UIImage imageNamed:@"Silver3.png"],
[UIImage imageNamed:@"Silver4.png"],
[UIImage imageNamed:@"Silver3.png"],
[UIImage imageNamed:@"Silver2.png"],
[UIImage imageNamed:@"Silver.png"],
[UIImage imageNamed:@"Silver5.png"],
[UIImage imageNamed:@"Silver6.png"],
[UIImage imageNamed:@"Silver7.png"],
[UIImage imageNamed:@"Silver6.png"],
[UIImage imageNamed:@"Silver5.png"],
[UIImage imageNamed:@"Silver.png"],
[UIImage imageNamed:@"Silver8.png"],
[UIImage imageNamed:@"Silver9.png"],
[UIImage imageNamed:@"Silver10.png"],
[UIImage imageNamed:@"Silver9.png"],
[UIImage imageNamed:@"Silver8.png"],
nil];
[Silver setAnimationRepeatCount:0];
Silver.animationDuration = 1.2;
[Silver startAnimating];
}
if (ScoreNumber > 999) {
Bronze.hidden = YES;
Silver.hidden = YES;
Gold.hidden = NO;
Diamond.hidden = YES;
Gold.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Gold.png"],
[UIImage imageNamed:@"Gold2.png"],
[UIImage imageNamed:@"Gold3.png"],
[UIImage imageNamed:@"Gold4.png"],
[UIImage imageNamed:@"Gold3.png"],
[UIImage imageNamed:@"Gold2.png"],
[UIImage imageNamed:@"Gold.png"],
[UIImage imageNamed:@"Gold5.png"],
[UIImage imageNamed:@"Gold6.png"],
[UIImage imageNamed:@"Gold7.png"],
[UIImage imageNamed:@"Gold6.png"],
[UIImage imageNamed:@"Gold5.png"],
[UIImage imageNamed:@"Gold.png"],
[UIImage imageNamed:@"Gold8.png"],
[UIImage imageNamed:@"Gold9.png"],
[UIImage imageNamed:@"Gold10.png"],
[UIImage imageNamed:@"Gold9.png"],
[UIImage imageNamed:@"Gold8.png"],
nil];
[Gold setAnimationRepeatCount:0];
Gold.animationDuration = 1.2;
[Gold startAnimating];
}
if (ScoreNumber > 4999) {
Bronze.hidden = YES;
Silver.hidden = YES;
Gold.hidden = YES;
Diamond.hidden = NO;
Diamond.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Diamond.png"],
[UIImage imageNamed:@"Diamond2.png"],
[UIImage imageNamed:@"Diamond3.png"],
[UIImage imageNamed:@"Diamond4.png"],
[UIImage imageNamed:@"Diamond3.png"],
[UIImage imageNamed:@"Diamond2.png"],
[UIImage imageNamed:@"Diamond.png"],
[UIImage imageNamed:@"Diamond5.png"],
[UIImage imageNamed:@"Diamond6.png"],
[UIImage imageNamed:@"Diamond7.png"],
[UIImage imageNamed:@"Diamond6.png"],
[UIImage imageNamed:@"Diamond5.png"],
[UIImage imageNamed:@"Diamond.png"],
[UIImage imageNamed:@"Diamond8.png"],
[UIImage imageNamed:@"Diamond9.png"],
[UIImage imageNamed:@"Diamond10.png"],
[UIImage imageNamed:@"Diamond9.png"],
[UIImage imageNamed:@"Diamond8.png"],
nil];
[Diamond setAnimationRepeatCount:0];
Diamond.animationDuration = 1.2;
[Diamond startAnimating];
}
}
- (void)viewDidLoad
{
ScoreNumber = 0;
ScoreLabel.text = [NSString stringWithFormat:@"0"];
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:@"%li", (long)HighScoreNumber];
}