我正在创建一个相对复杂的动画序列。其中,某个 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/