使用SKKeyFrameSequence值驱动自定义SKAction,如何?

时间:2023-01-22 20:55:16

For motion to be done by a custom SKAction, I want a much stronger ease type than is possible with traditional equation created curves.

对于由自定义SKAction完成的运动,我希望有一种比用传统的方程创建曲线更强的易解类型。

How do I use an SKKeyFrameSequence to drive animation in a SKAction?

如何使用SKKeyFrameSequence来驱动SKAction中的动画?

SKKeyFrameSequence creates the type of curve I want, like so:

SKKeyFrameSequence创建了我想要的曲线类型,如下所示:

let sequence = SKKeyframeSequence(
    keyframeValues: [0.00, 0.05, 0.25, 0.70, 0.85, 0.95, 0.98, 1.00],
    times:          [0.00, 0.10, 0.15, 0.20, 0.23, 0.28, 0.50, 1.00])

sequence.interpolationMode = .spline
stride(from: 0.00, to: 1.00, by: 0.025).forEach {
let value = sequence.sample(atTime: CGFloat($0))

This looks like this, on a curve created in PlayGrounds:

这是这样的,在操场上形成的曲线:

使用SKKeyFrameSequence值驱动自定义SKAction,如何?

SKActions, the custom type, take in the elapsed time and the Node to apply changes to.

skaction,自定义类型,在经过的时间和节点上执行更改。

But I cannot conceive how to take SKKeyFrameSequence values and get them into the custom SKAction to transform the position, rotation or scale of the Node the custom Action is being run upon.

但是我无法想象如何获取SKKeyFrameSequence值并将它们转换到自定义SKAction中,以转换自定义操作正在运行的节点的位置、旋转或比例。

SK Custom Action Documentation: https://developer.apple.com/reference/spritekit/skaction/1417745-customaction

自定义操作文档:https://developer.apple.com/reference/spritekit/skaction/1417745-customaction

1 个解决方案

#1


1  

To use a custom action, it looks like you pass a block which sets the position to customAction(withDuration:actionBlock):

要使用自定义操作,看起来您传递了一个块,该块将位置设置为customAction(withDuration:actionBlock):

let action = SKAction.customAction(withDuration: duration) { node, elapsed in
    node.position.x = sequence.sample(atTime: elapsed/duration)
}

#1


1  

To use a custom action, it looks like you pass a block which sets the position to customAction(withDuration:actionBlock):

要使用自定义操作,看起来您传递了一个块,该块将位置设置为customAction(withDuration:actionBlock):

let action = SKAction.customAction(withDuration: duration) { node, elapsed in
    node.position.x = sequence.sample(atTime: elapsed/duration)
}