ios - SKAction 改变 anchor ?

标签 ios animation rotation sprite-kit anchorpoint

我正在创建一个相对复杂的动画序列。其中,某个 SKSpriteNode(鲨鱼)进行两次旋转。在动画开始时,它围绕某个 anchor ap1旋转,然后围绕不同 anchor ap2旋转。我应该如何在动画序列的中途更改 anchor ?

一些初步想法:

我可以在 update: 循环中更改 SKAction anchor 。

我可以对同一个鲨鱼 Sprite (及其各自的 anchor )使用多个 SKSpriteNode,当我需要更改 anchor 时切换(隐藏/显示) Sprite 节点。

最佳答案

由于更改 Sprite 的 anchor 会影响其渲染位置,因此您可能需要进行某种调整以防止 Sprite 看起来突然移动到新位置。这是一种方法:

创建改变 anchor 的 Action

    SKAction *changeAnchorPoint = [SKAction runBlock:^{
        [self updatePosition:sprite withAnchorPoint:CGPointMake(0, 0)];
    }];

    SKAction *rotate = [SKAction rotateByAngle:2*M_PI duration: 2];

运行旋转 Action ,改变 anchor ,旋转

    [sprite runAction:[SKAction sequence:@[rotate,changeAnchorPoint,rotate]] completion:^{
        // Restore the anchor point
        [self updatePosition:sprite withAnchorPoint:CGPointMake(0.5, 0.5)];
    }];

此方法调整 Sprite 的位置以补偿 anchor 的变化

- (void) updatePosition:(SKSpriteNode *)node withAnchorPoint:(CGPoint)anchorPoint
{
    CGFloat dx = (anchorPoint.x - node.anchorPoint.x) * node.size.width;
    CGFloat dy = (anchorPoint.y - node.anchorPoint.y) * node.size.height;
    node.position = CGPointMake(node.position.x+dx, node.position.y+dy);
    node.anchorPoint = anchorPoint;
}

关于ios - SKAction 改变 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27365797/

相关文章:

ios - CocoaPods 库中的 CoreData

javascript - 使用 CSS 定位自身以外的样式

ios - 如何观察图像旋转?

css - 将 CSS 转换/动画减慢到 "slow motion"

c# - 根据旋转的文本框旋转光标

Java旋转问题

ios - Core Location 正常工作的超时时间

iphone - 检测 iPhone/iPad 自动屏幕锁定设置

ios - 在 Swift 中访问字典内对象内的字典

jquery - 如何检查 gif 动画是否已完成