Howdy, Stranger!

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

Pause / Resume Tweens

edited January 2014 in General Posts: 2,043

I know there is a tween.stop function to stop a tween with a given id and a function to resume a tween with a given id. However, there is also a tween.stopAll function to stop all tweens, but no tween.playAll to resume all tweens. So my question is, how can I pause and then resume all active tweens? Is there a way to get the id of all tweens?


  • Posts: 1,595

    I suggest you create a class and table to store all the tweens created in the class then loop through the table, that way you can get set the id's yourself, it's also possible with a table instead of a class.

  • edited January 2014 Posts: 2,043

    Well, I want to be able to pause / resume tween of any project through a dependency. I guess I could overwrite tween and pass all id's to a handler... I was just hoping an existing function would save me the work :)

  • Posts: 2,043

    So I figured it out. tween is actually a table, and it appears the function called when you do tween() is actually tween.start(), so I simply overwrote tween.start with a function that stores the tween in a table, and then calls the original tween.start

  • Posts: 1,595

    Nicely done!

  • Jmv38Jmv38 Mod
    Posts: 3,297


  • When you say "overwrote tween.start", does that mean if you create your own function in your project named tween.start, Codea will use your own function instead of the in-built one?

  • IgnatzIgnatz Mod
    edited January 2014 Posts: 5,396

    @time_trial - yes - the way we normally do it is this

    --first make a copy of the original function
    --(it's actually just the address of the original function)
    --then write your function, and call the original function when you are ready
    function tween.start()
        --your code
        Old_tween.start() --call original function (assuming you want to)

    So when you use tween.start(), it will run your function instead of the original.

  • edited January 2014 Posts: 505

    Take a look at this, I think this is the code behind the tween lib in Codea. There you have all the docs…

  • Cool. Very useful!

  • Posts: 2,043

    I am trying to find the function called when a tween is finished but I cannot find it in Codea.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @JakAttak - thanks to the code link posted by se24vad, I found a function called hasExpiredTween.

    That function doesn't work, but the code inside it does. If your tween is called t, then this statement will become true when the tween finishes

    t.running >= t.time
  • Posts: 2,043

    @Ignatz thanks! I had found the tween.hasExpired function but it wasn't ever being called and I was confused. Now I simply do a check in the tween.stop function for whether t.running >= t.time so I know when to remove a tween from the storage table.

  • Posts: 2,043

    Well. Turns out I need to add even more stuff to handle paths and loops. You'd think it would be simpler.

  • SimeonSimeon Admin Mod
    Posts: 5,387

    @JakAttak that's the reason we haven't added official support for pause / resume. I still plan to modify the tween library to add it, but it doesn't yet work in all cases.

  • Posts: 2,043

    @Simeon, actually I figured out how to handle both cases properly.

  • toffertoffer Mod
    Posts: 151

    My 2 cents

    local update,noop = tween.update,function() end
    tween.pauseAll = function()
        tween.update = noop
    tween.resumeAll = function()
        tween.update = update
  • Posts: 2,043

    @toffer, I can't believe I didn't think of that... I rewrote so many functions, and you did it with 7 lines... =D>

  • toffertoffer Mod
    Posts: 151

    @JackAttack thank's @Ignatz - you make my day :)

Sign In or Register to comment.