2

我有这个代码:

@implementation MyScene {
SKAction *delayAction; 
}
Inside a method:

delayAction = [SKAction waitForDuration:3.0];
[self runAction:[SKAction repeatActionForever: [SKAction sequence:
                                                @[delayAction, [SKAction ...]]]]]
                                                  withKey:@"myKey"];

然后我想减少加班时间。(更新时调用此方法:)所以我尝试了:

    - (void)updateVelocity
{
    NSLog(@"duration:%f",delayAction.duration);
    delayAction.duration = delayAction.duration - 0.001;
}

我得到:

2014-04-04 11:45:05.781 FlyFish[5409:60b] duration:1.300000
2014-04-04 11:45:05.785 FlyFish[5409:60b] duration:1.299000
2014-04-04 11:45:05.800 FlyFish[5409:60b] duration:1.298000
2014-04-04 11:45:05.816 FlyFish[5409:60b] duration:1.297000

这看起来不错,但我的 [SKAction ...] 仍然在 3 秒后继续重复。

4

2 回答 2

7

我会以不同的方式做到这一点。像这样的东西...

- (void)recursiveActionMethod
{
    if (some end condition is met) {
        return;
        // this allows you to stop the repeating action.
    }

    self.duration -= 0.01;
    // store duration in a property

    SKAction *waitAction = [SKAction waitForDuration:self.duration];
    SKAction *theAction = [SKAction doWhatYouWantHere];
    SKAction *recursiveAcion = [SKAction performSelector:@selector(recursiveActionMethod) onTarget:self];

    SKAction *sequence = [SKAction sequence:@[waitAction, theAction, recursiveAction]];
    [self runAction:sequence];
}

这将执行您的操作,然后返回到此函数,以不同的等待时间再次运行,一次又一次,...

您甚至可以通过在 if 块内跳转并停止循环的一些结束条件来停止序列。

于 2014-04-04T11:09:13.963 回答
2

我有点晚了,但是您可以将操作设置为 SKActionTimingEaseOut。这也是语言原生的,应该工作得稍微快一些。(尽管您无法自定义速度更改)这可以与此类似:

  1. yourSKAction.timingMode = SKActionTimingEaseOut;
于 2015-03-06T18:07:14.457 回答