简体   繁体   English

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

[英]SKAction repeating each frame for count?

Does anyone know of a way in Sprite Kit to repeat an action each frame using a count? 有谁知道Sprite Kit中使用计数在每一帧重复动作的方法吗? I was hoping the example below would work but all the repeats happen in the same update cycle. 我希望下面的示例可以工作,但所有重复都发生在同一更新周期中。

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

Alternatively I looked at (See below) but this is based on a duration not a specified number of calls, so it all depends on your framerate (which I don't believe you can access). 或者,我看了一下(见下文),但这是基于持续时间而不是指定的呼叫次数,因此,这完全取决于您的帧速率(我认为您无法访问)。 If your running at 20FPS you get 10 "HELLO AGAIN!"s if your at 60FPS then you get 30. 如果您以20FPS的速度跑步,您将获得10“ HELLO AGAIN!”;如果您以60FPS的速度跑步,则您将获得30。

SKAction *helloAction = [SKAction customActionWithDuration:0.5 actionBlock:^(SKNode *node, CGFloat elapsedTime) {
    NSLog(@"HELLO AGAIN!");
}];
[self runAction:helloAction];

EDIT: 编辑:

It would seem that I was on the right track with SKAction repeatAction:count: , where I was going wrong was that NSLog(@"HELLO!"); 似乎我在使用SKAction repeatAction:count: ,但我出问题的地方是NSLog(@"HELLO!"); (or any calculation you might do in the block) is an instantaneous action. (或您可能在块中执行的任何计算)都是瞬时动作。 If this is the case Sprite Kit repeats the action on the current frame only. 在这种情况下,Sprite Kit仅在当前帧上重复操作。 The answer is to make the SKAction none instantaneous by adding a small delay via a sequence, now Sprite Kit executes the block once for each of the next 10 frames. 答案是通过在序列上添加一个小的延迟来使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:]

I think you can try the following: 我认为您可以尝试以下方法:

@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
}

method of the SKScene object, which is called exactly once per frame as long as the scene is presented in a view and is not paused. SKScene对象的方法,只要场景在视图中呈现且不暂停,则每帧精确调用一次。

By default, this method does nothing. 默认情况下,此方法不执行任何操作。 Your should override it in your scene subclass and perform any necessary updates to the scene. 您应该在场景子类中覆盖它,并对场景执行任何必要的更新。

SKScene Class Reference SKScene类参考

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

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