2014-02-17 136 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]; 
     } 
    }]; 
} 

任何帮助,非常感谢!

回答