Game Development Reference
In-Depth Information
Chapter
4
Adding Scene Scrolling and Game Control
In the previous chapter I discussed Sprite Kit's physics engine and collision detection. I
started with a discussion of SKPhysicsBody . You then turned on the game world's grav-
ity and added a touch responder to propel the playerNode up into space. Finally, I
closed out the chapter with a discussion of how to handle collisions.
In this chapter, you will start adding some real functionality to your game. You are going to
begin by making some small changes to the current GameScene . After that, you will add
additional orb nodes to collide into. You will then add scrolling to your scene, allowing you
to make it look like the player is flying through space collecting orbs. And finally, you will
start using the phone's accelerometer to move the player along the x-axis.
Reorganizing the GameScene
Before you move on to adding scrolling and the accelerometer, you need to do some small
code changes in the existing GameScene . In previous chapters, there was a lot of code
that showed how to do certain things in Sprite Kit. The changes you will be making here
are reorganizational changes focused on setting up the game for continued game develop-
ment.
The first thing you need to change is the strength of gravity in the game's physics world.
You are going to do this by changing the vector that represents gravity's force from (0.0,
-0.2) to (0.0, -0.5). Go ahead and find where you set the physicsWorld.gravity
property and change it to the following:
physicsWorld.gravity = CGVectorMake(0.0, -5.0)
To prepare the scene for the next section where you will start scrolling the different layers
in the game scene, you need to add another layer to your scene to hold all of the sprites.
You will do this by adding another SKSpriteNode named foregroundNode . This
node will hold all of the sprites that will affect game play. So, let's add this node and add
Search WWH ::




Custom Search