Game Development Reference
In-Depth Information
As you rotate the device, you can see that values change very fast, and different
device positions yield different results. For every person, that baseline will be
different, and we need to acknowledge that. To compensate that baseline, we will
read the starting value on the launch of an application and base our movement on it.
For our landscape mode game, reading the X axis' coordinates will be most useful.
To get it, we poll self.manager.accelerometerData.acceleration.x .
In order to get the data that we need, we have to save that baseline value.
This is where it gets interesting. Accelerometer data is not available straight
away when you ask it to start updating. It takes some time to poll hardware.
First, add @property (assign) float baseline; to ERGMyScene.h —we
will store the offset for accelerometer data here.
In the init method, right after the startAccelerometerUpdates method, add the
performSelector call to execute the method that sets the baseline:
[self performSelector:@selector(adjustBaseline) withObject:nil
afterDelay:0.1];
After this, we need to add the adjustBaseline method:
- (void) adjustBaseline
{
self.baseline = self.manager.accelerometerData.acceleration.x;
}
We will also need some value to be multiplied with the accelerometer data,
and having it as a magic number is never good, so let's add a new line to the
Common.h ile:
static NSInteger accelerometerMultiplier = 15;
Add the following code to the bottom of your update method in ERGMyScene.m :
ERGPlayer *player = (ERGPlayer *) [self childNodeWithName:playerName];
player.position = CGPointMake(player.position.x, player.position.y
- (self.manager.accelerometerData.acceleration.x - self.baseline) *
accelerometerMultiplier);
if (player.position.y < 68) {
player.position = CGPointMake(player.position.x, 68);
}
if (player.position.y > 252) {
player.position = CGPointMake(player.position.x, 252);
}
 
Search WWH ::




Custom Search