致命错误:索引超出范围:文件Swift / ContiguousArrayBuffer.swift

时间:2020-11-06 04:13:15

标签: ios swift

我在iOS游戏中使用过UIViewPropertyAnimator,但是,当游戏结束并且玩家退出游戏的View控制器时,在用户触摸另一个View控制器的屏幕后,应用程序将崩溃。它给了我以下错误:-

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)
  }
}

我不知道为什么退出游戏视图控制器后触摸屏幕后会崩溃。任何帮助将不胜感激!如果您需要任何其他信息,请随时提问!谢谢!

0 个答案:

没有答案