繁体   English   中英

SKAction重复每一帧进行计数吗?

[英]SKAction repeating each frame for count?

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

或者,我看了一下(见下文),但这是基于持续时间而不是指定的呼叫次数,因此,这完全取决于您的帧速率(我认为您无法访问)。 如果您以20FPS的速度跑步,您将获得10“ HELLO AGAIN!”;如果您以60FPS的速度跑步,则您将获得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:]

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

@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类参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM