简体   繁体   中英

Sounds are playing all at once although waitForCompletion = YES! - spritekit

I would like the sounds to play in order of the array. And if the first sound is finished the second should play, and so on...

Below you can see I wrapped the sounds in an array in a subclass:

   @implementation Level
    {
        SKAction *_s1;
        SKAction *_s2;
        SKAction *_s3;
        SKAction *_s4;
    }

    - (instancetype)init
    {
        self = [super init];
        if (self) {
            _s1 = [SKAction playSoundFileNamed:@"SoundOne.mp3" waitForCompletion:YES];
            _s2 = [SKAction playSoundFileNamed:@"SoundTwo.mp3" waitForCompletion:YES];
            _s3 = [SKAction playSoundFileNamed:@"SoundThree.mp3" waitForCompletion:YES];
            _s4 = [SKAction playSoundFileNamed:@"SoundFour.mp3" waitForCompletion:YES];

            _oneArray = @[_s1, _s1, _s2, _s2, _s3, _s4];


        }
        return self;
    }

Then I passed through the array with sounds to the main scene. As shown here:

@implementation GameScene
{
    Level *_level;
    NSArray *_levelOne;
}

-(void)didMoveToView:(SKView *)view {
    // setup level class
    _level = [[Level alloc] init];

    _levelOne = _level.oneArray;
    SKAction *one = [SKAction sequence:@[_levelOne]];
    [self runAction:one];

All help is appreciated.

This will play all sound 1 by 1

    [self runAction:[SKAction sequence:@[
                                    [SKAction playSoundFileNamed:@"SoundOne.mp3" waitForCompletion:true],
                                    [SKAction playSoundFileNamed:@"SoundOne.mp3" waitForCompletion:true],
                                    [SKAction playSoundFileNamed:@"SoundTwo.mp3" waitForCompletion:true],
                                    [SKAction playSoundFileNamed:@"SoundTwo.mp3" waitForCompletion:true],
                                    [SKAction playSoundFileNamed:@"SoundThree.mp3" waitForCompletion:true],
                                    [SKAction playSoundFileNamed:@"SoundFour.mp3" waitForCompletion:true],
                                     ]]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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