在 [SKAction playSoundFileNamed:waitForCompletion:] 之后等待

Wait after [SKAction playSoundFileNamed:waitForCompletion:]

好吧,这个让我觉得自己像个彻头彻尾的白痴。

我的游戏中有一种方法,旨在通过一次弹出一个字母来为文本框中的文本设置动画。在我的 DDTextBox class 中,有一个名为 animateText 的动画方法。这是它的编码:

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();

它的意思是让名为textBoxText的节点发生变化,所以它是字符串_text的子串,直到索引i,然后等到声音文件结束,直到它移动到下一个字符。

目前,声音播放一次,同时弹出所有文字。有问题的音频文件长约 200 毫秒。我该怎么做才能一遍又一遍地播放同一个文件?我会在 [self runAction:completion:] 的方法调用之外声明 SKAction 吗?有什么想法吗?

您可以通过创建一系列操作来做到这一点。您可能需要进行一些调整。

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

我还建议这样做只是为了简化代码:

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);