I have used UIViewPropertyAnimator in my iOS Game, but, when the game is over, and the player exits the game View controller, the app crashes after the user touches on the screen on the other view controller. It is giving me the following error:-
Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift
The code where it is giving me an error:-
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()
}
}
I have used the following function in my game view controller to detect the first touch to start the game:-
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)
}
}
I don't know why is it crashing after touching on the screen after exiting the game view controller. Any help would be highly appreciated! If you need any other information please feel free to ask! Thanks!