Krystian
Krystian

Reputation: 3423

Create custom transition in Corona SDK

I went through the docs and couldn't find anything. Is it possible to create a custom transition? I have to simulate a throw with custom easing etc. In cocos2d I was able to just create a custom action, with corona I'm puzzled.

Upvotes: 1

Views: 611

Answers (2)

Sebastian Mach
Sebastian Mach

Reputation: 39089

The docs are quite silent on the topic. However, if you do some trying out, you will find out that a custom easing function takes four parameters (only nil from the fifth parameter and up). Some playing around reveals that a custom easing function looks like this:

function easer(currentTime, duration, startValue, targetDelta)
    ...
end

Explanation

  • currentTime / duration: duration is self-explanatory, and currentTime is simply [0..duration] during the course of the transition.
  • startValue: Snapshot of the value at the beginning of the transition.
  • targetDelta: This is the value of the target-value, minus the startValue, i.e. the distance the easing-function "has to walk"

Annotated Example

Say you have the following code:

foo.frob = 1
transition.to(foo, {time=1001, frob=0.25})

I.e., you want a transition of foo.frob of [1..0.25]. Then:

function easer(currentTime, duration, startValue, targetDelta)
    -- currentTime:  [0..1001]
    -- duration:     1001
    -- startValue:   1
    -- targetDelta:  to-from = 0.25-1 = -0.75

    return startValue + (currentTime/duration) * targetDelta -- a linear interpolator
end

Return Value

The return value should be startValue in the beginning, and startValue+targetDelta at the end. It is perfectly allowed to leave that range; however, once the transition stops, it automatically becomes startValue+targetDelta, so make it startValue and startValue+targetDelta at the beginning and end.

But you can be creative between the start and end, and let the function bounce back and forth, for example, like some of the included easing functions already do.

Upvotes: 1

Krystian
Krystian

Reputation: 3423

Well the answer is in the docs.

One of the params to transition.to is easing, which can be a custom function.

Upvotes: 0

Related Questions