I have the following custom SKAction working but as an EaseIn instead EaseOut. I want it to EaseOut! I have failed miserably to correct it using various easing equations found around the web.
let duration = 2.0
let initialX = cameraNode.position.x
let customEaseOut = SKAction.customActionWithDuration(duration, actionBlock: {node, elapsedTime in
let t = Double(elapsedTime)/duration
let b = Double(initialX)
let c = Double(targetPoint.x)
let p = t*t*t*t*t
let l = b*(1-p) + c*p
node.position.x = CGFloat(l)
})
cameraNode.runAction(customEaseOut)
Any help would be much appreciate.
Thanks
You don't need to calculate it.
SKAction
just have a property called timingMode
:
// fall is an SKAction
fall.timingMode = .easeInEaseOut
You can choose from:
Check details from API docs and also here.
If you need to change the Apple presets you can use: timingFunction
fall.timingFunction = { time -> Float in
return time
}
To build a custom function according to the source:
/**
A custom timing function for SKActions. Input time will be linear 0.0-1.0
over the duration of the action. Return values must be 0.0-1.0 and increasing
and the function must return 1.0 when the input time reaches 1.0.
*/
public typealias SKActionTimingFunction = (Float) -> Float
So with these informations you can write:
func CubicEaseOut(_ t:Float)->Float
{
let f:Float = (t - 1);
return f * f * f + 1;
}
fall.timingFunction = CubicEaseOut
We can modify the following code to allow the custom action to ease-out instead of ease-in
let t = Double(elapsedTime)/duration
...
let p = t*t*t*t*t
To get a better understanding of p
, it is helpful to plot it as a function of t
Clearly, the function eases in over time. Changing the definition of t
to
let t = 1 - Double(elapsedTime)/duration
and plotting p
gives
The action now eases out, but it starts at 1 and ends at 0. To resolve this, change the definition of p
to
let p = 1-t*t*t*t*t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With