我正在尝试使用陀螺仪制作一个简单的应用程序,其中一个角色根据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);
}
请帮帮我.. 并提前完成。
(编辑)
显然,motionManager.isDeviceMotionAvailable返回FALSE ... 这必须意味着iPad 1不支持CoreMotion ??? 这可能与设置有关吗?
答案 0 :(得分:4)
iPad第一代支持CMMotionManager
(因为它有一个加速度计),但不会返回任何陀螺仪数据 - 它没有陀螺仪!您需要检查gyroAvailable
实例的CMMotionManager
属性。