首页 文章

SKAction 重复每一帧计数?

提问于
浏览
0

有没有人知道 Sprite Kit 中的一种方法,每次使用计数重复一个动作?我希望下面的示例可以工作,但所有重复都发生在同一个更新周期中。

SKAction *helloAction = [SKAction runBlock:^{
    NSLog(@"HELLO!");
}];
SKAction *repeatBlock = [SKAction repeatAction:helloAction count:10];
[self runAction:repeatBlock];

OUTPUT:

[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];
}

OUTPUT:

[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:]

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

相关问题