0

有谁知道 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:]
4

1 回答 1

0

我认为您可以尝试以下方法:

@property (nonatomic) NSInteger frameCounter;

- (void)didEvaluateActions
{
     if (self.frameCounter < 10){
          [self runAction:(SKAction *) yourAction];
          self.frameCounter++;
     }
     // frameCounter to be set to 0 when we want to restart the update for 10 frames again
}

SKScene 对象的方法,只要场景在视图中呈现并且没有暂停,每帧只调用一次。

默认情况下,此方法不执行任何操作。您应该在场景子类中覆盖它并对场景执行任何必要的更新。

SKScene 类参考

于 2014-01-21T15:38:43.250 回答