Accelerometer not working after uploading game to the App Store
I made a space game where you use the accelerometer to move a spaceship, When I try the game on my devices (iPad, iPhone) it work well with no errors or incorrect behavior.
I upload my game to the App Store and it was approved, but when I downloaded the game from the 开发者_C百科App Store, the accelerometer wasn't working and my spaceship was stuck in one direction.
I tried many devices and they all have same problem.
Note: I used the cocos2d and box2d frameworks.
If anybody have any idea, please help!
Use UIAccelerometerDelegate
UIAccelerometer *accelerometer;
In .m file
# pragma mark To Enable Acceleromete
self.isAccelerometerEnabled = YES;
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = 0.025;
self.accelerometer.delegate = self;
Use these delegates methods
#pragma Mark Accelerometer
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// Set up variables
CGSize winSize = [CCDirector sharedDirector].winSize;
#define kFilteringFactor 0.5
#define kShipMaxPointsPerSec (winSize.height*0.5)
#define kRestAccelX (xCallib)
#define kMaxDiff 0.2
#define kRestAccelY (yCallib)
#define kMaxDiffY 0.1
UIAccelerationValue rollingX=0;
float accelX;
// High pass filter for reducing jitter
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
accelX = acceleration.x - rollingX;
// Calculate movement for x and y axis
float accelDiffX = accelX - kRestAccelX;///
float accelFractionX = accelDiffX / kMaxDiff;//
movementX = kShipMaxPointsPerSec * accelFractionX;
// Thresh holds for x and y axis movement
willMoveX = YES;
if (((movementX < 45.0f) && (movementX > -45.0f)))
willMoveX = NO;
}
#pragma Mark Accelerometer Update Methods
-(void) update:(ccTime)dt
{
CCSprite *player =(CCSprite *) [self getChildByTag:objPlayerShipTag];
CGSize screenSize = [[CCDirector sharedDirector]winSize];
float oldX = [player position].x;
float newX;
if (willMoveX) {
newX = [player position].x + (movementX * dt);
} else newX = oldX;
if ((newX > (screenSize.width -45)) || newX < 45.0f ) {
newX = oldX;
}
[player setPosition:ccp(newX,90)];
// ++++++++ To generate Bullet and Bomb Power +++++++
[self checkForCollisionWithPowersBullets]; //NEW
[self checkForCollisionWithPowersBomb];
}
精彩评论