Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tween Question

edited February 2013 in General Posts: 398

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?

Comments

  • Posts: 489

    Hello @andymac3d. Is the following of any help?

    --
    -- tween.running
    --
    
    function setup()
        w, h = WIDTH / 2, HEIGHT / 2
        fontSize(72)
        subject = {x = 0}
        local target = {x = 5}
        tweenId = tween(10, subject, target, "quadInOut")
    end
    
    function draw()
        background(0)
        local t = string.format("%1.2f", tweenId.running/tweenId.time)
        text(t, w, h) 
    end
    
  • 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 :-)

    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?

  • SimeonSimeon Admin Mod
    Posts: 4,927

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

  • edited February 2013 Posts: 489

    The fields of the tweenId table are:

    time
    running
    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 :-)

Sign In or Register to comment.