使用最短角度旋转SpriteNode

时间:2014-09-11 20:53:22

标签: ios objective-c rotation sprite-kit skspritenode

现在我的代码旋转了_eye以查看_me但是当_eye从359度旋转到1度时它不会通过359旋转 - > 0 - > 1,但它是359 - > 358 - > .. - > 2 - > 1。

#define SK_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f)
#define SK_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint location = [touch locationInNode:self];
        [_me runAction:[SKAction moveTo:location duration:0.5]];
    }
}

-(void)update:(CFTimeInterval)currentTime
{
    float deltaX = _me.position.x - _eye.position.x;
    float deltaY = _me.position.y - _eye.position.y;
    float angle = atan2f(deltaY, deltaX);

    [_eye runAction:[SKAction rotateToAngle:angle - SK_DEGREES_TO_RADIANS(90.0f) duration:0.2]];
}

1 个答案:

答案 0 :(得分:2)

要以最小的角度旋转精灵,请将shortestUnitArc添加到SKAction:

[_eye runAction:[SKAction rotateToAngle:angle - SK_DEGREES_TO_RADIANS(90.0f) duration:0.2 shortestUnitArc:YES]];

从Apple的文档中

  

shortestUnitArc

     

如果是,则以任何方式执行旋转   方向导致最小的旋转。如果否,则旋转   是插值的。