Game Development Reference
In-Depth Information
The reason the background is so much bigger is because it is going to scroll downward to
simulate the playerNode flying up through space. This is going to be accomplished by
making use of the update() method in the game's rendering loop.
The first step to scroll the background is to change the position of the background based
upon the location of the player in the game. Every time the player goes higher in the
scene, the background will be moved down in the scene. The following code, which you
will need to add to the GameScene in a moment, does just this:
override func update(currentTime: NSTimeInterval) {
self.backgroundNode!.position =
CGPointMake(self.backgroundNode!.position.x,
-((self.playerNode!.position.y - 180.0)/8))
}
This implementation of the update() method changes the position of the background
node based upon the current position of the player. Specifically, it sets the position of the
backgroundNode to its same x-value but uses a y-value that is 180 points below the
position of the player, which is then divided by 8. Don't worry about these numbers at the
moment. I will talk about them a lot more in just a moment. Go ahead and add this update
method to the end of the GameScene and then run the game again.
When you tap the screen this time, you will see the background slowly move toward the
bottom of the scene as the player flies off the top of the screen. That's pretty cool, but the
player is still flying out of the scene. There needs to be one more change made to the up-
date() method. Take a look at this modified update() :
override func update(currentTime: NSTimeInterval) {
if playerNode!.position.y >= 180.0 {
backgroundNode!.position =
CGPointMake(backgroundNode!.position.x,
-((playerNode!.position.y - 180.0)/8))
foregroundNode!.position =
CGPointMake(foregroundNode!.position.x,
-(playerNode!.position.y - 180.0))
}
}
Notice there have been two changes made. First the method is checking to see whether the
playerNode has moved at least 180 points up the scene. If the playerNode has
Search WWH ::




Custom Search