Xcode 5 ntimer倒计时移除2条生命而不是1条
本文关键字:生命 1条 2条 ntimer 倒计时 Xcode | 更新日期: 2023-09-27 18:06:36
你好,我正在尝试制作一款高级测验游戏,每回答一个问题倒数5秒,并在5秒内回答10分,3秒内回答5分,1秒内1个但我的主要问题是计数器起作用了我调用的一个方法来删除一个直播并开始错误答案的方法都起作用了但突然间,它删除了2个生命而不是1个并且超出了我设定的0的上限游戏结束时是-2个生命我不知道有人能帮我吗?会很感激这么多和一点菜鸟在编码。这是我使用的代码在我的游戏里。M文件
///////计时器//////////////
-(void) timerRun {
secondsCount = secondsCount - 1;
//int minuts = secondsCount /60;
int seconds = secondsCount;// - (minuts * 60);
NSString *timerOutput = [NSString stringWithFormat:@"%.2d" , seconds];
countDownLaber.text = timerOutput;
if (secondsCount == 0) {
[countDownTimer invalidate];
countDownTimer = nil;
[self LoseByTime];
}
}
-(void) setTimer{
secondsCount = 5;
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
///////////////计时器 ///////////////////
-(void)LoseByTime{
LivesNumber = LivesNumber - 1;
Lives.text = [NSString stringWithFormat:@"%i" , LivesNumber];
}
-(void)RightAnswer{
ScoreNumber = ScoreNumber + 50;
Score.text = [NSString stringWithFormat:@"%i" , ScoreNumber];
NextCategory.hidden = NO;
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
Answer4.hidden = YES;
QuestionText.hidden = NO;
CategorySelected.hidden = YES;
//Result.hidden = NO;
//Result.image = [UIImage imageNamed:@"transparentPic.png"];
}
-(void)WrongAnswer{
LivesNumber = LivesNumber - 1;
Lives.text = [NSString stringWithFormat:@"%i" , LivesNumber];
NextCategory.hidden = NO;
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
Answer4.hidden = YES;
QuestionText.hidden = NO;
CategorySelected.hidden = YES;
//Result.hidden = NO;
//Result.image = [UIImage imageNamed:@"transparentPic.png"];
if (LivesNumber == 0) {
Result.image = [UIImage imageNamed:@"gameOver.png"];
NextCategory.hidden = YES;
Exit.hidden = NO;
GameInProgress = NO;
}
}
- (IBAction)Answer1:(id)sender;{
if (Answer1Correct == YES) {
[self RightAnswer];
right1.hidden = NO;
}
else{
[self WrongAnswer];
wrong1.hidden = NO;
}
}
- (IBAction)Answer2:(id)sender;{
if (Answer2Correct == YES) {
[self RightAnswer];
right2.hidden = NO;
}
else{
[self WrongAnswer];
wrong2.hidden = NO;
}
}
- (IBAction)Answer3:(id)sender;{
if (Answer3Correct == YES) {
[self RightAnswer];
right3.hidden = NO;
}
else{
[self WrongAnswer];
wrong3.hidden = NO;
}
}
- (IBAction)Answer4:(id)sender;{
if (Answer4Correct == YES) {
[self RightAnswer];
right4.hidden = NO;
}
else{
[self WrongAnswer];
wrong4.hidden = NO;
}
}
在setTimer中,我建议取消并清除countDownTimer,以确保一次只存在一个计时器。由于计时器目前只在秒数为零时被清除,因此有可能在此发生之前调用setTimer。
您好,感谢您的回复,我目前通过添加[countDownTimer invalidate]解决了这个问题;countDownTimer = nil;我的其他方法以及我的"一个错误的答案"在if中== 0我猜它似乎起了作用它只在计时器为0时才使计时器失效如果你输入了错误的答案,它不会停止计时器,所以输入错误的答案不会停止计时器,计时器计数到0,然后移除live,所以即使调用了另一个方法,它也必须使计时器失效虽然我仍然没有弄清楚如何使分数和计时器得分取决于你回答的时间:/