Pause / Resume Tweens

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?

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.

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 :slight_smile:

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

Nicely done!


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?

@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.

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!

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

@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

@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.

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

@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.

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

My 2 cents

local update,noop = tween.update,function() end
tween.pauseAll = function()
    tween.update = noop
tween.resumeAll = function()
    tween.update = update

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

@toffer - it made a nice post for me

@JackAttack thank’s @Ignatz - you make my day :slight_smile: