0

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!

Pressing_Keys_24_7
  • 1,755
  • 2
  • 7
  • 33
  • Umm... haven't you posted this question before? – aheze Nov 06 '20 at 04:14
  • @aheze yes, but I didn' t recieve an answer and I am still not able to resolve the issue. – Pressing_Keys_24_7 Nov 06 '20 at 04:16
  • Ok. Do you get anything besides `Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift`? Like which line is crashing – aheze Nov 06 '20 at 04:17
  • Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444 – Pressing_Keys_24_7 Nov 06 '20 at 04:19
  • 1
    Weird, this seems to be an increasingly common error as posted [here](https://stackoverflow.com/questions/63383459/swiftui-fatal-error-index-out-of-range-file-swift-contiguousarraybuffer-swift) and [here](https://www.reddit.com/r/SwiftUI/comments/ib50r2/fatal_error_index_out_of_range_file/) – aheze Nov 06 '20 at 04:24
  • 1
    @aheze is it to do something with the Swift/ContiguousArrayBuffer.swift library? – Pressing_Keys_24_7 Nov 06 '20 at 04:27
  • 1
    Possibly, that's a vague error but from the other posts it seems like it has something to do with arrays. It may be because of `event?.allTouches?.first?`, try playing around with that, maybe unwrapping the optionals with `if let` instead of optional chaining – aheze Nov 06 '20 at 04:31
  • @aheze Alright thanks for the help! I will give it a shot! – Pressing_Keys_24_7 Nov 06 '20 at 04:34
  • 1
    You iterate over `enemyViews` and within the loop you access `enemyAnimators` collection using loop's `index`. Is it possible these collections don't have the same number of items? – Dima G Nov 07 '20 at 20:21
  • @DimaG Thanks for your reply! I was able to resolve the issue, as I wasn't dismissing the view controller after performing the segue, thus the following error occurred! – Pressing_Keys_24_7 Nov 07 '20 at 21:17

0 Answers0