Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 229
shahidaltaf Avatar asked Oct 14 '16 12:10

shahidaltaf


2 Answers

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:

  • linear (default)
  • easeIn
  • easeOut
  • easeInEaseOut

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
like image 163
Alessandro Ornano Avatar answered Oct 06 '22 16:10

Alessandro Ornano


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

like image 2
Epsilon Avatar answered Oct 06 '22 16:10

Epsilon