2012-03-08 6 views
0

自分のiPhone 4からCore Motionでジャイロスコープのデータを取得したいだけですが、常にロール= 0、ピッチ= 0、ヨー= 0が得られます。 は、集中的な検索後、私は、私はiPhone 4 上でそれを実行しますが、私は設定でジャイロを有効にする必要がありますか、誤ったものiPhone 4にジャイロスコープはありませんか?

if (motionManager.isDeviceMotionAvailable) 
{ 
    [motionManager startDeviceMotionUpdates]; 
} 

収益を認識しましたか?それとも私は何が間違っていますか?

@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; 
} 
+0

ITは私のために働きます。 'motionManager'が' nil'でないこと、そしてあなたがシミュレータではなくデバイス上で実際に動作していることを確認してください。 – sch

+0

'motionManager'はどのように作成しますか?あなたはコードを投稿できますか? – Saphrosit

+0

私はmotionManagerを初期化するいくつかのコードを追加しました...多分それは私を助けるのに役立ちます:) – clash

答えて

1

にこのコードを使用して、私の減少インタフェースザッツ

現在のデバイスでCMMotionManagerが使用可能かどうかを確認します。これがCMMotionManagerインスタンスの初期化に失敗した場合は、iPhone Simulatorなどのジャイロなしのデバイスでアプリを実行していることがわかります:

// check if the motion manager class is available (available since iOS 4.0) 
Class motionManagerClass = NSClassFromString(@"CMMotionManager"); 
if (motionManagerClass) 
{ 
    motionManager = [[motionManagerClass alloc] init]; 
} 
else 
{ 
    NSLog(@"CMMotionManager not available"); 
} 
関連する問題