簡體   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