Game Development Reference
In-Depth Information
Some games benefit greatly from accelerometer support, and we are going to
implement this in our game.
Accelerometer data is available only on devices. Thus, if you test it on the
simulator, this code won't do anything, as the accelerometer will return
0 all the time. Consider getting an Apple developer license if you have
not got one already, as it offers the option of testing on a real device.
You can get it at https://developer.apple.com/programs/ios/ .
It is especially useful for developing games, as the frame rate in Sprite
Kit in the simulator is far from the one that you get on real devices.
The simulator uses a much faster CPU and much faster memory, but the
emulated rendering pipeline is much slower, and you can get very low
fps in most trivial situations. Don't trust the frame rate in the simulator!
The accelerometer is handled by the Core Motion library, so you will need to
include that in your project. To do that, simply add @import CoreMotion; ,
where all imports are at the top of the ERGMyScene.h ile.
@import is the new way to import frameworks into your code, and it was
introduced with iOS 7 and Xcode 5. Earlier, you had to add a framework
to your project and use #import everywhere you needed it. Now you
can use @import and forget about adding a framework in the project's
settings. The only drawback to this is that you can only use the Apple
frameworks this way. Let's hope it will change to all libraries some day.
The next thing that you need to do is add the Core Motion manager as a property
to your scene.
To do that, add the following line to ERGMyScene.h :
@property (strong, nonatomic) CMMotionManager *manager;
The manager handles all accelerometer data, and we need it to use the
accelerometer's input. In order to start getting accelerometer data, we need
to call a few more methods.
In the initWithSize: method of ERGMyScene.m , add the following lines:
self.manager = [[CMMotionManager alloc] init];
self.manager.accelerometerUpdateInterval = 0.1;
[self.manager startAccelerometerUpdates];
 
Search WWH ::




Custom Search