我的游戏每秒播放滴答声。我希望声音在整个游戏过程中慢慢加快。我最初的想法是使用 NSTimer 并在方法触发时更新速度,如下所示:
static float soundDelay = 1.0;
timer = [NSTimer scheduledTimerWithTimeInterval:clickClackDelay
target:self
selector:@selector(playSound)
userInfo:nil
repeats:YES];
- (void)playSound {
soundDelay -= 0.1;
NSLog(@"Play sound");
}
这没有用,而且看起来 NSTimer 并不是真的应该以这种方式使用。关于如何实现此目标的任何其他建议?
最佳答案
您可以通过从自身调用playSound
方法来实现它。
您可以通过以下方式进行。
- (void)playSound
{
static float soundDelay = 1.0;
if([timer isValid])
{
[timer invalidate];
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:clickClackDelay
target:self
selector:@selector(playSound)
userInfo:nil
repeats:NO];
soundDelay -= 0.1;
if(soundDelay <=0) //when sound delay is zero invalidate timer
{
[timer invalidate];
timer = nil;
}
NSLog(@"Play sound");
}
关于objective-c - iOS:以逐渐增加的速度触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064220/