Ios 致命错误:索引超出范围:文件Swift/ContiguousArrayBuffer.Swift
我在iOS游戏中使用过UIViewPropertyAnimator,但是,当游戏结束,玩家退出游戏视图控制器时,当用户触摸另一个视图控制器的屏幕后,应用程序崩溃。它给了我以下错误:-Ios 致命错误:索引超出范围:文件Swift/ContiguousArrayBuffer.Swift,ios,swift,Ios,Swift,我在iOS游戏中使用过UIViewPropertyAnimator,但是,当游戏结束,玩家退出游戏视图控制器时,当用户触摸另一个视图控制器的屏幕后,应用程序崩溃。它给了我以下错误:- Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift 给我一个错误的代码:- func moveEnemies(to touchLocation: CGPoint) { for (index, enemyView) in
Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift
给我一个错误的代码:-
func moveEnemies(to touchLocation: CGPoint) {
for (index, enemyView) in enemyViews.enumerated() {
let duration = getEnemyDuration(enemyView: enemyView)
enemyAnimators[index] = UIViewPropertyAnimator(duration: duration,
curve: .linear,
animations: {
enemyView.center = touchLocation
})
enemyAnimators[index].startAnimation()
}
}
我在游戏视图控制器中使用了以下功能来检测第一次触摸以启动游戏:-
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// First touch to start the game
if gameState == .ready {
startGame()
}
if let touchLocation = event?.allTouches?.first?.location(in: view) {
// Move the player to the new position
movePlayer(to: touchLocation)
// Move all enemies to the new position to trace the player
moveEnemies(to: touchLocation)
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
//第一次触按开始游戏
如果游戏状态==.ready{
startGame()
}
如果let touchLocation=事件?.alltouchs?.first?.location(在:视图中){
//将玩家移动到新位置
移动播放器(至:触摸位置)
//将所有敌人移动到新位置以追踪玩家
移动敌人(至:触摸位置)
}
}
我不知道为什么在退出游戏视图控制器后触摸屏幕后它会崩溃。任何帮助都将不胜感激!如果您需要任何其他信息,请随时询问!谢谢 嗯。。。你以前没有发布过这个问题吗?奇怪的是,这似乎是一个越来越常见的错误,@aheze是否与Swift/ContiguousArrayBuffer.Swift库有关?可能,这是一个模糊的错误,但从其他帖子来看,它似乎与数组有关。这可能是因为
event?.alltouch?.first?
,尝试使用打开选项,如果让而不是可选链接,您可以在enemyViews
上迭代,并在循环中使用循环的索引访问enemyAnimators
集合。这些集合是否可能没有相同数量的项目?