2011-08-27 84 views
2

我试图做一个简单的应用程序利用陀螺仪,其中一个字符根据iPad的1的iPad 1陀螺仪:滚转,俯仰,偏航留零

我的代码是不能工作的旋转而移动,所以我测试看到原始,俯仰,偏航, 的值,他们实际上保持为零,但我移动设备。 我敢肯定的iPad 1支持CMMotionManager,所以我不知道是什么导致它... 我的代码如下

- (id) init{ 
if((self=[super init])){ 
    self.isTouchEnabled = YES; 
    winSize = [[CCDirector sharedDirector] winSize]; 
    [self createRabbitSprite]; 

    self.motionManager = [[CMMotionManager alloc] init]; 
    motionManager.deviceMotionUpdateInterval = 1.0/60.0; 
    if(motionManager.isDeviceMotionAvailable){ 
     [motionManager startDeviceMotionUpdates]; 
    } 
    [self scheduleUpdate]; 
    //[self registerWithTouchDispatcher]; 
} 
return self; 

}

-(void)update:(ccTime)delta{ 
CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion; 
CMAttitude *currentAttitude = currentDeviceMotion.attitude; 

if(referenceFrame){ 
    [currentAttitude multiplyByInverseOfAttitude:referenceFrame]; 
} 

float roll = currentAttitude.roll; 
float pitch = currentAttitude.pitch; 
float yaw = currentAttitude.yaw; 

NSLog(@"%.2f and %.2f and %.2f",roll,pitch,yaw); 

rabbit.rotation = CC_RADIANS_TO_DEGREES(yaw); 

}

请帮助我出.. 和thanx提前。

(编辑)

显然,motionManager.isDeviceMotionAvailable被返回FALSE ...... 必须意味着iPad的1不支持CoreMotion ??? 它可能是与设置的东西?

+0

您可以参考此答案:http://stackoverflow.com/a/10145614/1336105 –

回答

4

iPad的第一代支持CMMotionManager(因为它有一个加速度计),但不会返回任何陀螺仪数据 - 它没有陀螺仪!您需要检查CMMotionManager实例的gyroAvailable属性。