OGeek|极客世界-中国程序员成长平台

标题: ios - 等待 [SKAction playSoundFileNamed :waitForCompletion:] [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:41
标题: ios - 等待 [SKAction playSoundFileNamed :waitForCompletion:]

好吧,这个让我觉得自己像个白痴。

我在我的游戏中有一个方法,旨在通过使文本框中的文本一次弹出一个字母来为文本设置动画。在我的 DDTextBox 类中,有一个方法可以对其进行动画处理,称为 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 吗?有什么想法吗?



Best Answer-推荐答案


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

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

关于ios - 等待 [SKAction playSoundFileNamed :waitForCompletion:],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691320/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4