有谁知道 Sprite Kit 中使用计数重复每帧动作的方法?我希望下面的示例能够正常工作,但所有重复都发生在同一个更新周期中。
SKAction *helloAction = [SKAction runBlock:^{
NSLog(@"HELLO!");
}];
SKAction *repeatBlock = [SKAction repeatAction:helloAction count:10];
[self runAction:repeatBlock];
输出:
[14972:70b] -[MyScene update:]
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] HELLO!
[14972:70b] -[MyScene update:]
[14972:70b] -[MyScene update:]
[14972:70b] -[MyScene update:]
[14972:70b] -[MyScene update:]
或者,我查看了(见下文),但这是基于持续时间而不是指定的调用次数,因此这完全取决于您的帧速率(我不相信您可以访问)。如果您以 20 FPS 运行,您将获得 10 个“HELLO AGAIN!”,如果您以 60 FPS 运行,您将获得 30 个。
SKAction *helloAction = [SKAction customActionWithDuration:0.5 actionBlock:^(SKNode *node, CGFloat elapsedTime) {
NSLog(@"HELLO AGAIN!");
}];
[self runAction:helloAction];
编辑:
看起来我在正确的轨道上SKAction repeatAction:count:
,我出错的地方是NSLog(@"HELLO!");
(或你可能在块中进行的任何计算)是一个瞬时动作。如果是这种情况,Sprite Kit 只会在当前帧上重复该动作。SKAction
答案是通过序列添加一个小的延迟来使无瞬时完成,现在 Sprite Kit 会为接下来的 10 帧中的每一帧执行一次该块。
- (void)testMethod {
SKAction *myAction = [SKAction runBlock:^{
NSLog(@"BLOCK ...");
}];
SKAction *smallDelay = [SKAction waitForDuration:0.001];
SKAction *sequence = [SKAction sequence:@[myAction, smallDelay]];
SKAction *repeatSequence = [SKAction repeatAction:sequence count:10];
[self runAction:repeatSequence];
}
输出:
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]
[399:60b] BLOCK ...
[399:60b] -[MyScene update:]