每当玩家收集硬币时,它就是声音,但是一旦角色接触到硬币,游戏就会冻结并退出。
func playTap() {
if let url = Bundle.main.url(forResource: "tap", withExtension: "caf"){
tap = try AVAudioPlayer(contentsOf: url)
guard let tap = tap else { return }
tap.prepareToPlay()
tap.play()
} else {
print(error.localizedDescription)
}
}
答案 0 :(得分:0)
我猜你的代码正在崩溃,因为你正在尝试 FORCE UNWRAP一个空的可选,并在行的末尾加上!。
let url = Bundle.main.url(forResource: "tap", withExtension: "caf")!
尝试
if let url = Bundle.main.url(forResource: "tap", withExtension: "caf"){
//Code here
} else {
}
或
guard let url = Bundle.main.url(forResource: "tap", withExtension: "caf") else {}
修改强>
拼写检查键值!!