Game Development Reference
In-Depth Information
moved this high, then the backgroundNode is moved down the scene at 1/8th the
speed of the player, and then the foreground is moved at exactly the same rate as the play-
er. Moving the foreground at the same rate as the player prevents the player from going
too high and leaving the scene. Change the current update() method to look like this
one and run the application again.
When you tap the screen this time, the background moves slowly down the screen, and the
SuperSpaceMan does not fly off the top of the screen. This is great except for one thing.
When the playerNode gets to the top of the first line of orbs, the player can go no high-
er because the next set of orbs are to the right, and the player has no way of getting to
them. This problem will be solved in the next section of this chapter when I show you
how to add horizontal movement to the player using the accelerometer.
Controlling Player Movement with the Ac-
celerometer
In the previous section of this chapter, the player ran into a problem when the next set of
orbs needed to continue up the scene were 50 points to the right but the player had no ho-
rizontal movement to reach them. You will now fix this problem by using the phone's ac-
celerometer to control the player's movement along the x-axis.
To use the accelerometer in your game, you first need to add the CoreMotion framework
to your GameScene . You do this by adding the following import statement to the top
of your GameScene.swift file:
import CoreMotion
After this, you need to add two properties. The first will hold an instance to the CMMo-
tionManager object that will be used to monitor horizontal movement, and the second
will hold a CGFloat value representing the acceleration along the x-axis. Add these two
variables to the GameScene directly after the impulseCount variable added earlier:
let coreMotionManager = CMMotionManager()
var xAxisAcceleration : CGFloat = 0.0
This code creates an instance of the CMMotionManager and stores it in the constant
coreMotionManager . The next line creates a CGFloat variable, xAxisAcceler-
ation , and initializes it with the value 0.0.
Search WWH ::




Custom Search