每当玩家收集硬币时,它就会发出声音,但是一旦角色接触到硬币,游戏就会冻结并退出。
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)
}
}
最佳答案
我猜您的代码崩溃了,因为您试图用强制强制取消使用null可选的!该行末尾的。
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 {}
编辑
拼写检查关键值!
关于swift - 运行此功能时游戏崩溃?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41051261/