Tracking the player's progress
First, we need to keep track of how far the player has flown. We will use this later as well, for keeping track of a high score. This is easy to implement. Follow these steps to track how far the player has flown:
In the
GameScene.swiftfile, add two new properties to theGameSceneclass:let initialPlayerPosition = CGPoint(x: 150, y: 250) var playerProgress = CGFloat()
In the
didMovefunction, update the line that positions the player to use the newinitialPlayerPositionconstant instead of the old hardcoded value:// Add the player to the scene: player.position = initialPlayerPosition
In the
didSimulatePhysicsfunction, update the newplayerProgressproperty with the player's new distance:// Keep track of how far the player has flown playerProgress = player.position.x - initialPlayerPosition.x
Perfect! We now have access to the player's progress at all times in the GameScene class. We can use the...