0

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

一些初步的想法:

我可能会在循环中更改s之外的锚点。SKActionupdate:

我可以SKSpriteNode为同一个鲨鱼精灵使用多个 s(具有它们各自的锚点),当我需要更改锚点时切换(隐藏/显示)精灵节点。

4

2 回答 2

1

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

创建改变锚点的动作

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

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

运行动作以旋转、更改锚点和旋转

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

此方法调整精灵的位置以补偿锚点的变化

- (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;
}
于 2014-12-08T21:11:39.120 回答
0

正如Julio Montoya所说,最简单的方法就是将代码“翻译”成SKActionwith the method [SKAction runBlock:myBlock]

于 2014-12-08T20:06:50.393 回答