(NSTimer)创建计时器倒数 [英] (NSTimer) creating a Timer Countdown

查看:133
本文介绍了(NSTimer)创建计时器倒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的倒数计时器,以便当玩家进入我的游戏时,计时器从60倒数到0.这看似简单,但是我对如何编写它感到困惑.

I am trying to create a simple Countdown Timer so that when a player enters my game the timer begins from 60 down to 0. It seems simple but I am getting confused at how I write this.

到目前为止,我已经在GameController.m中创建了一个如下所示的方法:

So far I have created a method within my GameController.m that looks like this:

-(int)countDownTimer:(NSTimer *)timer {
    [NSTimer scheduledTimerWithTimeInterval:-1
                                 invocation:NULL
                                    repeats:YES];
    reduceCountdown = -1;
    int countdown = [[timer userInfo] reduceCountdown];
    if (countdown <= 0) {
        [timer invalidate];
    }
    return time;
}

在游戏开始时,我将整数Time初始化为60.然后在ViewController中设置标签.但是在我编译代码的那一刻,它只在60处显示标签,而丝毫不减.

At the start of the game I initialise the integer Time at 60. The label is then being set within ViewController. But at the moment when I compile the code it just shows the label at 60 and doesn't decrement at all.

任何帮助将不胜感激-我是Objective-C的新手.

Any help would be greatly appreciated - I am new to Objective-C.

编辑

在我的帮助下,我现在将代码分为2个单独的方法.现在的代码如下所示:

With some assistance from I have now separated the code into 2 separate methods. The code now looks like this:

-(void)countDown:(NSTimer *)timer {
    if (--time == 0) {
        [timer invalidate];
        NSLog(@"It's working!!!");
    }
}

-(void)countDownTimer:(NSTimer *)timer {
    NSLog(@"Hello");
    [NSTimer scheduledTimerWithTimeInterval:1
                                      target:self
                             selector:@selector(countDown:)
                                      userInfo:nil
                                      repeats:YES];
}

但是,该代码仍无法正常运行,当我从View Controller调用方法[game countDownTimer]时,它会中断说:无法识别的选择器已发送至实例".谁能解释这里出什么问题了?

HOWEVER, the code is still not running properly and when I call the method [game countDownTimer] from my View Controller it breaks saying: "unrecognized selector sent to instance". Can anybody explain what is wrong here?

推荐答案

您的代码有些错误:

  • 您在时间间隔中传递了错误的参数-负数被解释为0.1毫秒
  • 您正在调用错误的重载-您应该传递一个调用对象,但您传递的是NULL
  • 您将要在计时器上执行的代码与计时器初始化一起放置-需要在计时器上执行的代码应放入单独的方法中.
  • You are passing a wrong parameter for the time interval - negative numbers are interpreted as the 0.1 ms
  • You are calling the wrong overload - you are expected to pass an invocation object, yet you are passing a NULL
  • You put the code that you want executed on timer together with timer initialization - the code that needs to be executed on timer should go into a separate method.

您应该调用带有选择器的重载,并在间隔内传递1而不是-1.

You should call the overload that takes a selector, and pass 1 for the interval, rather than -1.

声明NSTimer *timerint remainingCounts,然后添加

timer = [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(countDown)
                                       userInfo:nil
                                        repeats:YES];
remainingCounts = 60;

到您要开始倒计时的地方.然后添加countDown方法本身:

to the place where you want to start the countdown. Then add the countDown method itself:

-(void)countDown {
    if (--remainingCounts == 0) {
        [timer invalidate];
    }
}

这篇关于(NSTimer)创建计时器倒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆