SKAudioNode plays sound once

I can not find much information about SKAudioNode. How can I play sound only once? I do not want to repeat the sound.

What I'm trying to do is play a short laser sound every time a bullet appears in spritekit.

+4
source share
2 answers

Unfortunately, what @KnightOfDragon says is wrong (but I don't have enough reputation for comments).

SKAudioNodewas introduced in iOS 9 and is intended to be replaced SKAction.playSoundFileNamed(...)because it is much more powerful (you can add it as a child in SpriteKit SKNode, and if the attribute positionalis set to true, adding 3D sound is automatically added).

SKAudioNode, :

if #available(iOS 9, *) {
    let pling = SKAudioNode(fileNamed: "pling.wav")
    // this is important (or else the scene starts to play the sound in 
    // an infinite loop right after adding the node to the scene).
    pling.autoplayLooped = false
    someNode.addChild(pling)
    someNode.runAction(SKAction.sequence([
        SKAction.waitForDuration(0.5),
        SKAction.runBlock {
            // this will start playing the pling once.
            pling.runAction(SKAction.play())
        }
    ])
}
else {
    // do it the old way
}

Update

, : SKAction- > Audio-Stuff. :

SKAction playSoundFileNamed: waitForCompletion: . AVAudioPlayer .

, , a SKAudioNode . playSoundFileNamed / ..

+9

, SKAction.playSoundFileNamed(...) , . SKAudioNode .

:

//we have ship as an SKSpriteNode

//lets fire laser

ship.runAction(SKAction.playSoundFileNamed("pewpewpew.caf",waitForCompletion:false));
+4

Source: https://habr.com/ru/post/1620767/


All Articles