如何沿着具有可变速度的CGPath移动精灵

Jus*_*ins 4 ios sprite-kit

我正在编写游戏并希望沿固定路径移动精灵(直道和曲线 - 想想铁路引擎)但我希望能够根据精灵速度驱动动画 - 并根据游戏事件改变速度.

followPath:duration:在SKAction中很接近,但似乎没有办法"动态"调整速度 - 我绝对关心精灵速度 - 不是'跟踪路径的持续时间'.

CGPath似乎是用于定义精灵路径的正确构造,但似乎没有足够的功能来沿路径抓取点并进行自己的数学运算.

有谁能建议合适的方法?

0x1*_*41E 5

您可以使用其speed属性调整任何SKAction的执行速度.例如,如果设置action.speed = 2,则操作将以两倍的速度运行.

SKAction *moveAlongPath = [SKAction followPath:path asOffset:NO orientToPath:YES duration:60];
[_character runAction:moveAlongPath withKey:@"moveAlongPath"];
Run Code Online (Sandbox Code Playgroud)

然后你可以调整角色的速度

[self changeActionSpeedTo:2 onNode:_character];
Run Code Online (Sandbox Code Playgroud)

改变SKAction速度的方法......

- (void) changeActionSpeedTo:(CGFloat)speed onNode:(SKSpriteNode *)node
{
    SKAction *action = [node actionForKey:@"moveAlongPath"];
    if (action) {
      action.speed = speed;
    }
}
Run Code Online (Sandbox Code Playgroud)