Tween Question

Love the new Tween library, a welcome addition and nicely integrated into the existing Codea suite of tools. The easing functions are fantastic! Well done chaps!

Was curious if there’s a way of querying the current state of a Tween? More specifically, if there is an easy way of returning a normalised percentage (i.e 0 → 1) of how far the Tween has got when interpolating between Value A and Value B over time?

I’m trying to refactor my existing game code which uses a much less sophisticated Tween type library that I wrote, but returns a normalised percentage value which I use a fair bit on each update cycle.

Any thoughts?

Hello @andymac3d. Is the following of any help?

-- tween.running

function setup()
    w, h = WIDTH / 2, HEIGHT / 2
    subject = {x = 0}
    local target = {x = 5}
    tweenId = tween(10, subject, target, "quadInOut")

function draw()
    local t = string.format("%1.2f", tweenId.running/tweenId.time)
    text(t, w, h) 

Think that’s nailed it @mpilgrem - thanks. Basically, it’s simply elapsed time/duration (as per my original code) but on a tween by tween basis. Nice :slight_smile:

Out of curiosity, the docs mention that the Tween function returns the ‘TweenId’ as a table of which it seems 'running and ‘time’ are return values within this. Is this documented anywhere? Couldn’t seem to easily find a reference to this in the docs.

What other useful values are returned?

It’s not documented, we probably should document the returned type from tween() as running/time seems very useful.

The fields of the tweenId table are:

initial -- table (the initial state of subject)
subject -- table
target -- table
easing -- function
args -- table (the optional arguments to the callback function)
loop -- function (tween.loop.once by default)

Incredibly useful stuff guys - thanks :slight_smile: