How do tweens work?


(Noah) #1

I’ve been trying to figure out how to get tweens to work in flashpunk and can’t find any good tutorials other than the api documentation.

Can someone explain how to work them for me? Thanks


(Martí Angelats i Ribera) #2

There is the base class Tween nad then multiple utility tweens. Let’s start with the base class.


After you create a tween, you use start() to start the time before calling the function. finish() to end the function beforehand manually (calls the complete function). cancel() to delete the tween from the container (doesn’t call the complete function).

The constructor has 4 parameters (all optional except the duration).

  1. The duration in seconds of the period. (aka time between calling the function)
  2. The type of the Tween (what does after finishing the first loop). LOOPING will be looping utill you stop or delete it. PERSIST will loop once every time you call start(). ONESHOT will call once and then delete iteself from the container (if you want to use it again it must be added to another tweener).
  3. The function tha is called after the full period. (you can call this function using the complete atribute if you don’t want to stop the tween, cancel() or 'finish()` (both of them explained above).
  4. This is a function that, in case that it exists, it’s used to modify the internal time. It must contain a single parameter which is a number and will be the current time; and returns another number wich is the new current time.

Here an example of creation a Tween and adding it to a Tweener (in this case [and the most common one] a World).

//somewhere inside a World function

var tween:Tween = new Tween (10, Tween.LOOPING, callback);
//Every 10 seconds 'callback' will be called, untill we finish() it or concel() it.

addTween(tween, true);
//this true starts the tween automatically, so we don't need another code line

But it doesn’t end here. The real thing with the tweens is that you can make subclasses with a better specific functionality. There already are a lot of usefull ones so i recomend to look the net.flashpunk.tweeners subpackages (if you want to see how they work you can look the code).


I hope it helped you even if it was a bit. I’m not the best trying to explain things, so sorry if you can’t understan it well.