Game Development Reference
In-Depth Information
}
You get the spaceman node using the presentationNode() method so
you can get the current position of him.
Next, using a little math and SCNVector3 initializer, you position the cam-
era above and behind the spaceman.
While you are here, you light him up so that he is the star of the game.
Now would be a good time to start up the game and check your progress. Once the game
starts, notice that when you touch the screen, your hero moves forward some distance.
Next, when you touch with two fingers, your hero moves backward a certain distance.
This is great, but now you need to have your hero move left and right as well. For this you
will use the accelerometer to capture the tilting of the device.
Since this topic is for beginners and Scene Kit, I will not go into too much detail about the
CoreMotion framework; however, you will use that framework in order to access the
device's accelerometer. If you want to learn more about the CoreMotion framework, the
Apple documents are a good place to start.
CoreMotion Framework Introduction
The step is to import the CoreMotion framework into your GameViewController
class using import CoreMotion .
With CoreMotion imported, you can create another class-level variable that will store
the CoreMotion manager. The reason you do this is because once you set up the CoreMo-
tion manager, you will create an NSOperationQueue that will run in the background
and detect the movements from the accelerometer. After you have the class declaration,
add var motionManager: CMMotionManager .
Now you can create a method that will set up the accelerometer and capture the input
from the accelerometer, similar to Listing 14-4 .
Listing 14-4 . setupAccelerometer Function
func setupAccelerometer() {
// Create the motion manager to receive the input
motionManager = CMMotionManager()
Search WWH ::




Custom Search