Я хочу, чтобы моя система начисления очков начиналась с определенного числа.

Привет, я использую Xcode для приложения, и моя система подсчета очков увеличивается на 1 балл каждую секунду. Как сделать так, чтобы оно начиналось с 25, а затем увеличивалось с 1 пункта в секунду. Это код:

-(void)Scoring{

ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:@"Score: %i", ScoreNumber];



-(void)NewGame{

ScoreNumber = 0;
Score.text = [NSString stringWithFormat:@"Score: 0"];

Пожалуйста помоги!!

2 ответа

В NewGameВы устанавливаете ScoreNumber на 0. Установите вместо этого 25:

-(void)NewGame{

ScoreNumber = 25;
Score.text = [NSString stringWithFormat:@"Score: 25"];

Попробуй это:

- (void) newGameWithStartingScore:(int)startingScore {

    // Set our label
    Score.text = [NSString stringWithFormat:@"Score: %i", startingScore];
    // Store our score in our dictionary
    NSDictionary * userInfo = [@{@"currentScore" : @(startingScore)} mutableCopy];
    // Start the timer
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(incrementScore:) userInfo:userInfo repeats:YES];
}

-(void)incrementScore:(NSTimer *)timer {

    // Get current score
    int currentScore = [timer.userInfo[@"currentScore"] intValue];
    // Increment it
    currentScore++;
    // Update userInfo ref
    timer.userInfo[@"currentScore"] = @(currentScore);

    // Set our label
    Score.text = [NSString stringWithFormat:@"Score: %i", currentScore];

    // See if timer should continue
    BOOL shouldInvalidate = NO;
    /*
     Insert validation logic here.  Set 'shouldInvalidate' to YES in order to stop the timer
     */
    if (shouldInvalidate) {
        [timer invalidate];
    }

}

При условии, что Score это ссылка на метку или что-то, что может отображать текст, вы можете назвать это так:

[self newGameWithStartingScore:25];

Таким образом, вы можете изменить его по мере необходимости.

Другие вопросы по тегам