Play Sound in Swift SpriteKit project?

后端 未结 2 2030
悲&欢浪女
悲&欢浪女 2021-01-13 02:19

When I run this code.

runAction(SKAction.playSoundFileNamed(\"Click.mp3\", waitForCompletion: false))

My app was Crashed:

2条回答
  •  青春惊慌失措
    2021-01-13 02:47

    verify that the file exists in the project. instantiates the variables in the file first and then make a method for breeding.

    if the file exists in the project you should do the following:

    var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
    ...
    override func didMoveToView(view: SKView) {
        playSound(sound)
        //call playSound method when you want
    }
    
    func playSound(sound : SKAction)
    {
        runAction(sound)
    }
    

    in this mode don't crash

提交回复
热议问题