2

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

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

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

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:

enter image description here

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

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.

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

Confused
  • 6,048
  • 6
  • 34
  • 75

1 Answers1

1

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

let action = SKAction.customAction(withDuration: duration) { node, elapsed in
    node.position.x = sequence.sample(atTime: elapsed/duration)
}
NobodyNada
  • 7,529
  • 6
  • 44
  • 51