I want to add a feature to my game that displays the player's Average Score in the Game Over scene. I took the approach of trying to add the final scores into an array, then divide by array.count and then display it on the next scene "Game Over". So far no luck, any help would be much appreciated.
Asked
Active
Viewed 81 times
0
-
1What do you mean by 'No luck'? what is the problem? – Charles-olivier Demers Jan 07 '16 at 01:30
1 Answers
0
First declare you an array:
let myArray = [CGFloat]()
After that before your game over append the score of the player:
myArray.append(playerScore)
Where you show your average, do:
for score in myArray {
sum += score
}
average = sum / myArray.count
And display the result!

Charles-olivier Demers
- 998
- 7
- 24
-
Charles, I have done what you said however the score from previous games do not save in the array. The array only holds the score of the game that was just played. It does not store all previous scores. – John Iannuzzi Jan 08 '16 at 03:52
-
Make the array a global variable! Create a swift file and add the array declaration there. Global variable are not the best way, but give it a try. Also you can declare your array in your main class and pass it as a parameter to your game over screen. – Charles-olivier Demers Jan 08 '16 at 03:54
-
Use [NSUserDefaults](http://stackoverflow.com/questions/25420651/store-string-in-nsuserdefaults-swift) to save the scores between games. – Epsilon Jan 08 '16 at 07:45
-
Yes if you want to save the score when the player close the game like @Elipson say, NSUserDefaults is à good idea! – Charles-olivier Demers Jan 08 '16 at 14:43