Game Development Reference
In-Depth Information
In the first line, we instantiate the manager, and in the second one, we set the update
interval—how often the values in the motion manager will be updated. On the third
line, we tell the manager to start updating, as it won't do anything before that.
Using the accelerometer increases the power use of the device.
Heavy use will drain the battery faster, thus making your
game unlikely to be played further. Only use the accelerometer
when you actually need to, and stop polling it if something
stops the application or if you don't need to parse the
accelerometer data at this time. You can use [self.manager
stopAccelerometerUpdates]; to stop the accelerometer.
Accelerometer data can be read from manager.accelerometerData , and it consists
of three fields—X, Y, and Z axis angle. Each of them represents a value from -1 to 1.
Y axis
Z axis
X axis
Accelerometer data axes
As you can see in the previous diagram, there are three axes, and rotating the device
around each of them changes only that axis' values. You can see this if you add the
following line to the update: method of ERGScene :
NSLog(@"%@", self.manager.accelerometerData);
In order to see what is going on, run the application and start rotating the device
along a different axis, and you will see the value of that go from -1 to 1. We will
use the accelerometer data to move the character sprite. Remember that the
accelerometer does not work on the simulator.
How can we do that? We may read the accelerometer data and set the sprite position
to certain values based on that. But how do we set these positions? We need some
baseline value so that our character stands on the ground.
 
Search WWH ::




Custom Search