每5秒加速一次下降的图像+0.1速度

时间:2014-02-28 23:56:02

标签: objective-c

只是一个快速的问题;)

下降图像的速度是:

int speed = 5; // pixel per update (i.e. 60 updates/second = 60 pixels/second)

我如何每5秒增加+ 0.01速度?

1 个答案:

答案 0 :(得分:0)

由于您已将速度声明为整数,因此无法向其添加0.01。首先,您必须将速度声明为浮点数。其次,您必须设置NSTimer,如下所示:

NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(timerLoop) userInfo:nil repeats:YES];

然后你必须编写一个函数来执行你想要每秒执行60次的任何事情。

- (void)timerLoop {
    // Add 0.01 to speed every time the function is called.
    speed += 0.01;
}

但请注意,因为NSTimer的分辨率不足以完全执行每1/60秒。如果您尝试为游戏循环执行此操作,这可能会导致帧速率问题。这就是我最初处理iOS游戏SlothDrop(https://itunes.apple.com/us/app/slothdrop-free/id789603341?mt=8&uo=4)中的位置和速度的方法。后来我继续使用CADisplayLink。