Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我的音效循环代码是什么_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 我的音效循环代码是什么

Ios 我的音效循环代码是什么,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在做一个有趣的游戏,我想让我的音效循环每次结束,这些是我的代码到目前为止 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches{ let touchLocation = touch.location(in: self) if atPoint(touchLocation).name == "startG

我正在做一个有趣的游戏,我想让我的音效循环每次结束,这些是我的代码到目前为止

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let touchLocation = touch.location(in: self)
        if atPoint(touchLocation).name == "startGame"{
            let gameScene = SKScene(fileNamed: "GameScene")!
            gameScene.scaleMode = .aspectFill
            view?.presentScene(gameScene, transition: SKTransition.doorsOpenHorizontal(withDuration: TimeInterval(2)))
            self.run(SKAction.playSoundFileNamed("mainmenu.wav", waitForCompletion: false))
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
让touchLocation=touch.location(in:self)
如果在点(touchLocation).name==“StartName”{
让GameSecene=SKScene(文件名为:“GameSecene”)!
gameScene.scaleMode=.aspectFill
查看?.presentScene(游戏场景,过渡:SKTransition.doorsOpenHorizontal(持续时间:时间间隔(2)))
self.run(SKAction.playSoundFileNamed(“main menu.wav”,waitForCompletion:false))

不能使用SKActions在场景中播放背景音乐。请改用AVAudioPlayer:

if let filePath = Bundle.main.path(forResource: "mainmenu", ofType: "wav") {
    let url = URL(fileURLWithPath: filePath)
    do {
        let player = try AVAudioPlayer(contentsOf: url)
        player.numberOfLoops = -1
        player.play()
    } catch {
        // catch error if necessary
    }
}
原职:

在游戏场景中,或您希望音乐循环的任何其他场景中使用此

 let music  = SKAudioNode(fileNamed: "backgroundmusic.mp3")

  music.autoplayLooped = true
    addChild(music)

就我个人而言,我会选择
AVAudioPlayer
SKAudioNode
,因为
playSoundFileNamed
在许多SpriteKit发行版中都变成了bug,但我将向您展示如何通过操作实现这一点:

import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        run(SKAction.repeatForever(SKAction.playSoundFileNamed("sound", waitForCompletion: true)))
    }
}