简体   繁体   English

[SKAction playSoundFileNamed:waitForCompletion:]之后等待

[英]Wait after [SKAction playSoundFileNamed:waitForCompletion:]

Alright, this one makes me feel like a complete idiot. 好吧,这让我觉得自己像个白痴。

I have a method in my game that's meant to animate the text in a text box by making it pop up one letter at a time. 我的游戏中有一种方法可以使文本框中的文本一次弹出一个动画,从而使文本动画。 In my DDTextBox class, there's a method that animates it called animateText . 在我的DDTextBox类中,有一种对其进行动画处理的方法称为animateText Here's the coding of it: 这是它的编码:

for (NSUInteger i = 1; i <= _text.length; i++)
    {
        [_scene runAction:[SKAction playSoundFileNamed:@"regularTextBeep.m4a" waitForCompletion:YES] completion:^{
            [_scene runAction:[SKAction waitForDuration:0.2] completion:^{
                ((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).text = (NSString*)[_text substringToIndex:i];

                ((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).position = CGPointMake(20 + (((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).frame.size.width / 2.0), ((SKLabelNode*)[_scene childNodeWithName:@"insideOfTextBox"]).frame.size.height - (((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).frame.size.height / 2.0) - 15);
                NSLog(@"Textbox loading a letter on iteration %u", i);
            }];
        }];
    }
    completion();

It's meant to make the node named textBoxText change so it's the substring of the string _text up to an index i , then wait until the sound file is over until it moves on to the next character. 这是为了使名为textBoxText的节点textBoxText更改,因此它是字符串_text的子字符串, 直至索引i ,然后等待声音文件结束,直到移至下一个字符。

Currently, the sound plays once, and all of the text pops up at the same time. 目前,声音只播放一次,所有文本同时弹出。 The audio file in question is about 200 ms long. 该音频文件的长度约为200毫秒。 How would I make it so it plays the same file over and over again? 我将如何使其反复播放相同的文件? Would I declare the SKAction outside of the method call for [self runAction:completion:] ? 我可以在[self runAction:completion:]的方法调用之外声明SKAction吗? Any ideas? 有任何想法吗?

You can do so by creating a sequence of actions. 您可以通过创建一系列操作来做到这一点。 You might need to make some adjustments. 您可能需要进行一些调整。

NSMutableArray *marrActions = [NSMutableArray new];

for (NSUInteger i = 1; i <= _text.length; i++)
{

    [marrActions addObject:[SKAction playSoundFileNamed:@"regularTextBeep.m4a" waitForCompletion:YES]];
    [marrActions addObject:[SKAction waitForDuration:0.2]];
    [marrActions addObject:[SKAction runBlock:^{
        ((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).text = (NSString*)[_text substringToIndex:i];

        ((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).position = CGPointMake(20 + (((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).frame.size.width / 2.0), ((SKLabelNode*)[_scene childNodeWithName:@"insideOfTextBox"]).frame.size.height - (((SKLabelNode*)[_scene childNodeWithName:@"textBoxText"]).frame.size.height / 2.0) - 15);
        NSLog(@"Textbox loading a letter on iteration %u", i);
    }]];
}

[_scene runAction:[SKAction sequence:marrActions] completion:completion]; //Pass off the completion block here

I would also suggest this just to simplify that code: 我还建议这样做只是为了简化该代码:

SKLabelNode *textNode = (SKLabelNode*)[_scene childNodeWithName:@"textBoxText"];
SKLabelNode *insideOfTextNode = (SKLabelNode*)[_scene childNodeWithName:@"insideOfTextBox"];

textNode.text = [_text substringToIndex:i];
textNode.position = CGPointMake(20 + (textNode.frame.size.width / 2.0), insideOfTextNode.frame.size.height - (textNode.frame.size.height / 2.0) - 15);

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

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