我正在制作一个有趣的游戏,我想在每次结束时让我的音效循环播放,这些是我到目前为止的代码。
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))
最佳答案
您无法使用 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
}
}
关于ios - 我的音效循环的代码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900762/