【发布时间】:2012-03-08 20:20:55
【问题描述】:
我只想从我的 iPhone 4 中获取一些使用 Core Motion 的陀螺仪数据,但总是得到 roll=0、pitch=0 和 yaw=0。 经过深入搜索,我意识到
if (motionManager.isDeviceMotionAvailable)
{
[motionManager startDeviceMotionUpdates];
}
返回 false,尽管我在 iPhone 4 上运行它。 我必须在设置中启用陀螺仪吗?或者我会做错什么……?
这是我的简化界面:
@interface GameLayer : CCLayer {
CMMotionManager *motionManager;
}
@property (nonatomic, retain) CMMotionManager *motionManager;
@end
这就是我实现motionManager的地方:
- (id) init
{
self=[super init];
if(self)
{
self.motionManager = [[[CMMotionManager alloc] init] autorelease];
motionManager.deviceMotionUpdateInterval = 1.0/60.0;
if (motionManager.isDeviceMotionAvailable)
{
[motionManager startDeviceMotionUpdates];
}
else
{
NSLog(@"No motion captured");
}
[self scheduleUpdate];
}
return self;
}
以及它被调用的循环:
- (void) update:(ccTime)delta
{
CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion;
motionManager.showsDeviceMovementDisplay = YES;
CMAttitude *currentDeviceAttitude = currentDeviceMotion.attitude;
float roll = currentDeviceAttitude.roll;
float pitch = currentDeviceAttitude.pitch;
float yaw = currentDeviceAttitude.yaw;
}
【问题讨论】:
-
它适合我。检查
motionManager不是nil,并且您实际上是在设备上而不是模拟器上运行。 -
如何创建
motionManager?可以发一下代码吗? -
我添加了一些代码,在那里我初始化了motionManager……也许这对我有帮助:)
标签: iphone objective-c cocos2d-iphone gyroscope core-motion