hey there,
accessing the accelerometer is part of iOS, not cocos2d, which is probably why there are no tets for it. Here is the function for iphone/ipad. not sure what it would be for android phone, probably similar enough though.
I use the code to dynamically change gravity based on the tilt of the device, but only within a certain range. Should be a pretty good example if you read through it carefully. Hope this helps :)
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Portrait values
if (currentLevel >= 1 && currentLevel <= 10) {
if (acceleration.x <= 0.4 && acceleration.x >= -0.4)
{
float temp1 = acceleration.x * 30;
float tempy = -fabsf(acceleration.y * 30) + fabsf(acceleration.z * 30);
if (tempy > 30)
{
tempy = 30;
}
float temp2 = -1 * sqrtf(30*30 - temp1 * temp1);
b2Vec2 gravity(temp1, temp2);
_world->SetGravity(gravity);
} else if (acceleration.x > 0.4) {
b2Vec2 gravity(0.4 * 30, -28.0);
_world->SetGravity(gravity);
} else if (acceleration.x < -0.4) {
b2Vec2 gravity(-0.4 * 30, -28.0);
_world->SetGravity(gravity);
}
} else if (currentLevel >= 11 && currentLevel <= 20) {
if (acceleration.x <= 0.4 && acceleration.x >= -0.4)
{
float temp1 = acceleration.x * 20;
float tempy = -fabsf(acceleration.y * 20) + fabsf(acceleration.z * 20);
if (tempy > 20)
{
tempy = 20;
}
float temp2 = -1 * sqrtf(20*20 - temp1 * temp1);
b2Vec2 gravity(temp1, temp2);
_world->SetGravity(gravity);
} else if (acceleration.x > 0.4) {
b2Vec2 gravity(0.4 * 20, -18.7);
_world->SetGravity(gravity);
} else if (acceleration.x < -0.4) {
b2Vec2 gravity(-0.4 * 20, -18.7);
_world->SetGravity(gravity);
}
}
}
for more information I'd rad apple documentation, or actually, probably android documentation on it since it will be for that.