简体   繁体   English

SpriteKit SKAction.playSoundFile-waitForCompletion不等待

[英]SpriteKit SKAction.playSoundFile - waitForCompletion is not waiting

I am doing some SpriteKit game development in Swift2. 我正在Swift2中进行一些SpriteKit游戏开发。 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. 我有2个部分的音频句子(两个部分都是根据实际游戏情况计算的),我想在第1部分结束后再播放第2部分。

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? 但是,参数waitForCompletion: true是什么呢?

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? 当使用runAction(action: SKAction, completion block: () -> Void)时,我可以使用完成处理程序来解决此问题,但是,waitForCompletion参数又是什么呢?

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 . 您误解了waitForDuration的含义。 In the apple docs, this is what it means - 在Apple文档中,这意味着-

wait 等待
If true, the duration of this action is the same as the length of the audio playback. 如果为true,则此操作的持续时间与音频播放的长度相同。 If false, the action is considered to have completed immediately. 如果为false,则认为该操作已立即完成。

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. 在您的情况下,运行动作而不将其放入SKAction.sequence中意味着编译器同时执行两行代码。 The same result would be achieved if you set wait to false for action1 and ran my code. 如果将action1 wait设置为false并运行我的代码,将会获得相同的结果。

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

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