Search code examples
iosswiftsprite-kitskaction

EaseOut action with custom SKAction


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


Solution

  • 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

    enter image description here

    Clearly, the function eases in over time. Changing the definition of t to

    let t = 1 - Double(elapsedTime)/duration
    

    and plotting p gives

    enter image description here

    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
    

    enter image description here