SKAction重复每一帧计数?

时间:2014-01-21 15:28:41

标签: ios sprite-kit skaction

有没有人知道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!”,如果你的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:]

1 个答案:

答案 0 :(得分: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 Class Reference