Naftali Beder
Naftali Beder

Reputation: 1076

Using SKActionTimingFunction in Sprite Kit

I have a moving action in Sprite Kit, and I'm controlling the transition like this:

mySKAction.timingMode = SKActionTimingMode.EaseIn

I want to create a custom transition using SKActionTimingFunction, but I can't figure out how. Based on the documentation, I think a simple linear transition would look something like this:

func myTransition(time: Float) -> Float {
    return time
}

mySKAction.timingFunction = myTransition()

But I just can't make it work. It displays an error: if I put in time as the function's parameter as the documentation suggests, I get '(UnsafeMutablePointer) -> time_t' is not convertible to 'Float'.

Upvotes: 1

Views: 564

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130193

It looks like the issue here is that you're passing the return value of your method to the action's timingFuncton property, when it's expecting the function itself. SKActionTimingFunction is defined as follows:

typealias SKActionTimingFunction = (Float) -> Float

Which is a closure that takes a single Float as an argument, and returns a Float, and this is the type of object that the timingFunction property is expecting.

func myTransition(time: Float) -> Float {
    return time
}

mySKAction.timingFunction = myTransition // <- Omitted parens

Although it's worth noting that this can all be simplified down to the following.

mySKAction.timingFunction = { time -> Float in
    return time
}

Upvotes: 4

Related Questions