ios - 使用最短角度旋转 Sprite 节点

标签 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]];
}

最佳答案

要将 Sprite 旋转最小角度,请将 shortestUnitArc 添加到 SKAction 中:

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

来自 Apple 的文档,

shortestUnitArc

If YES, then the rotation is performed in whichever direction results in the smallest rotation. If NO, then the rotation is interpolated.

关于ios - 使用最短角度旋转 Sprite 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25796751/

相关文章:

objective-c - xCode:通过控制台访问对象的属性

ios - 点击段时停止 cellForRowAtIndexPath

objective-c - UIModalTransitionStyleFlipHorizo​​ntal laggy

javascript - 用于旋转下拉菜单的 Jquery/Javascript

ios - "Hide"ViewController 没有完全关闭它

iphone - iOS UIWebview 忽略 CSS 行高

iphone - 减小图像文件大小

ios - uicontainerview(容器 View )内的多个 subview Controller

使用 MediaCodec 创建的 Android 旋转表面

iphone - 如何捕获当前 View 屏幕截图并在代码中重用? (iPhone SDK)