2014-02-17 3 views
1

장치를 왼쪽이나 오른쪽으로 기울여 구를 이동하려고합니다. 내가 지금 data.acceleration.y -> Y 축을 말하는 동안 장치를 위아래로 기울여야한다는 것을 제외하고 지금 당장 가지고있는 코드가 작동하고 있습니까? 어떤 도움이 정말 감사합니다기울이기 장치로 노드 이동

motionManager = [[CMMotionManager alloc] init]; 
if ([motionManager isAccelerometerAvailable] == YES) { 
    [motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init] 
             withHandler:^(CMAccelerometerData *data, NSError *error) 
    { 
     float destX, destY; 
     float currentX = sphereNode.position.x; 
     float currentY = sphereNode.position.y; 
     BOOL shouldMove = NO; 

     if(data.acceleration.y < -0.25) { // tilting the device to the right 
      destX = currentX + (data.acceleration.y * kPlayerSpeed); 
      destY = currentY; 
      shouldMove = YES; 
     } else if (data.acceleration.y > 0.25) { // tilting the device to the left 
      destX = currentX + (data.acceleration.y * kPlayerSpeed); 
      destY = currentY; 
      shouldMove = YES; 
     } 
     if(shouldMove) { 
      SKAction *action = [SKAction moveTo:CGPointMake(destX, destY) duration:1]; 
      [sphereNode runAction:action]; 
     } 
    }]; 
} 

:

여기 내 코드입니다!

답변

2

실제로 Y 축은 장치의 상단과 하단에 맞춰져 있습니다. CMAccelerometerData 데이터 객체의 x 축을 찾을 가능성이 가장 높습니다.

자세한 내용은 Event handling guide for iOS : Motion Events 설명서에 설명되어 있습니다.

아마도 자습서 (예 : iOS Programming Recipe 19: Using Core Motion to Access Gyro and Accelerometer)를 살펴 보는 것도 도움이 될 수 있습니다.

+0

나는 y를 x로 변경하여 문제를 해결할 수있었습니다. 이 문서를 가져 주셔서 감사합니다! –