Xcode 5 NSTimer倒计时删除2个生命而不是1个

时间:2014-06-03 20:07:13

标签: c# ios xcode timer countdown

你好我正在尝试制作一个高级测验游戏,倒计时。每回答一个问题需要5秒钟,如果在5秒内回答,5分钟在3秒内回答1分钟,在1秒内回答1分,但我的主要问题是计数器工作和我打电话取消现场的方法并开始错误的答案方法一切正常,但突然有时候它会消除2个生命而不是1个超出我的设定限制0对于游戏超过-2个生命我无法弄明白可以请有人帮助我?在编码中会非常感激和一点点菜鸟。 继承人我使用的代码 在我的game.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;



    }

}

2 个答案:

答案 0 :(得分:0)

在setTimer中,我建议无效并清除countDownTimer,以确保一次只存在一个计时器。由于当秒计数为零时,当前仅清除计时器,因此在此之前可能会调用setTimer。

答案 1 :(得分:0)

嗨,谢谢你的答复,我目前通过添加[countDownTimer invalidate]来解决这个问题。         countDownTimer = nil; 对我的其他方法以及我的"错误的答案 并且在if lives == 0 并且它似乎做了诀窍我猜它只是在计时器为0时无效定时器如果你找错了答案就没有停止定时器所以打错了答案并没有停止正在计数的定时器为0,然后删除一个实时,所以即使调用另一个方法,也必须使计时器无效 虽然我仍然没有弄清楚如何根据你回答的时间让分数和计时器得分:/