1

我的角色需要左右移动和旋转。移动一切正常,但我对旋转不满意。我希望角色仅在向左或向右移动时旋转。当角色不动时,旋转需要停止。模仿平滑的滚动。

我尝试通过 旋转SKAction,见下文:

if(_shouldMoveToRight) {
    SKAction *moveAction = [SKAction moveTo:CGPointMake(_destX, _destY) duration:1];
    [_character runAction:moveAction];

    SKAction *rotateAction = [SKAction rotateByAngle:0.2 duration:1];
    [_character runAction:rotateAction];
}
if (_shouldMoveToLeft) {
    SKAction *moveAction = [SKAction moveTo:CGPointMake(_destX, _destY) duration:1];
    [_character runAction:moveAction];

    SKAction *rotateAction = [SKAction rotateByAngle:0.2 duration:1];
    [_character runAction:rotateAction];

这使得角色一直在移动。任何帮助表示赞赏!

搬家代码:

self.manager = [[CMMotionManager alloc] init];
if ([self.manager isAccelerometerAvailable] == YES) {
    [self.manager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                        withHandler:^(CMAccelerometerData *data, NSError *error)
     {
         _destX = 0.0, _destY = 0.0;
         _currentX = _character.position.x;
         _currentY = _character.position.y;

         _shouldMoveToRight = NO;
         _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;
         }
     }];
}
4

0 回答 0