1

我想通过左右倾斜手机来旋转我的角色,但它只在调用 DidMoveToView 时旋转一次。我需要在我的代码中更改什么?

self.manager = [[CMMotionManager alloc] init];
if ([self.manager isAccelerometerAvailable] == YES) {
    [self.manager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                        withHandler:^(CMAccelerometerData *data, NSError *error)
     {
         float destX = 0.0, destY = 0.0;
         float currentX = _character.position.x;
         float currentY = _character.position.y;

         BOOL shouldMoveToRight = NO;
         BOOL shouldMoveToLeft = NO;

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

             SKAction *rotateAction = [SKAction rotateToAngle:M_PI duration:1];
             [_character runAction:rotateAction];
         }
         if (shouldMoveToLeft) {
             SKAction *moveAction = [SKAction moveTo:CGPointMake(destX, destY) duration:1];
             [_character runAction:moveAction];

             SKAction *rotateAction = [SKAction rotateToAngle:-M_PI duration:1];
             [_character runAction:rotateAction];
         }
     }];
}
4

0 回答 0