简体   繁体   中英

SpriteKit SKAction.playSoundFile - waitForCompletion is not waiting

I am doing some SpriteKit game development in Swift2. I have a 2 part audio sentence (both parts are calculated based on the actual game situation) and I want to play part 2 after part 1 is finished.

These two lines of sound play immediately at the same time

self.runAction(SKAction.playSoundFileNamed("question-part-a.wav", waitForCompletion: true)
self.runAction(SKAction.playSoundFileNamed("question-part-b.wav", waitForCompletion: true)

But then, what is the parameter waitForCompletion: true good for?

I could work around this with a completion handler, when using the runAction(action: SKAction, completion block: () -> Void) but again, what then does the waitForCompletion parameter?

Maybe it is a complete missunderstanding on my side. Could anybody give me a hint?

Thanks in advance Cheers John

You misunderstood the meaning of waitForDuration . In the apple docs, this is what it means -

wait
If true, the duration of this action is the same as the length of the audio playback. If false, the action is considered to have completed immediately.

So if you want to run it in order, do this.

let action1 = SKAction.playSoundFileNamed("question-part-a.wav", waitForCompletion: true)
let action2 = SKAction.playSoundFileNamed("question-part-b.wav", waitForCompletion: true)
runAction(SKAction.sequence([
            action1,
            action2
            ]))

In your case, running the actions without putting them in an SKAction.sequence means that the compiler executes both lines of code simultaneously. The same result would be achieved if you set wait to false for action1 and ran my code.

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