back to krasimirtsonev.com
to blog's home page

Create your own tween manager class in AS3

There are some features in Flash that we can't work without. Tween classes are among the most used ones. They give you ability to animate objects without using the timeline, to change the animation fast and easy. The idea of these classes is very simple. That's why I think that it is a good idea to have your own tween manager that you can modify to fit into your needs.

The result of this article can be found here and the source files are available for download here.

The basic structure of our tween manager:

We are going to pass the object that we want to modify and the properties that we want to change. The idea is to create a function that calls every frame. All the magic will be done there. As you can see in the code below I created a public function - start, which adds listener for ENTER_FRAME event. So now we have a repeated method, i.e. loop.

What are we going to pass as properties parameter? I think it is a good idea to use JSON object, because it's really flexible and we can add/remove properties really fast without changing anything in our class. Here is an example of the creation of an object from our tween class.

There are two things that you have to notice. The first one is that you should have a movie clip on the stage that you can use for the tween. The second thing is the properties object. As you can see we are going to change the "x" property of the clip from 50 to 390 for 100 steps, i.e. 100 frames. To be able to do that we need the value of "x" for each one of these 100 frames. The function "calculateValues" will do that for us:

As you can see we are adding dynamically a new array for every property in our properties object. "values" is an array with all the values of "x" for each of these 100 frames. The last thing that we should make is to pass the values to our clip, i.e. to write the content of the "loop" method.

Please notice lines 18 and 19. We added a flag (isItDone) that indicates when the tween is finished and "valueIndex", which we used to go through all the values.
When you run the flash you will see that the circle is moving from x=50 to x=390 for 100 frames. Let's add some other properties and see how it looks:

We can change as many properties as we want and obviously to set different steps for each one of them.

Ok, the Tween class looks good so far but it isn't as cool as we wanted. It's because there is no easing. We used only a linear type of motion. To add other types we're going to use some maths by Robert Penner. We will just change the calculateValues method and it will support different types of motions. Here is the final version of the class:

And the usage:

Together with "start", "end" and "steps" we are passing a new property "method" which is used by the "calculateValues" function. The class "Ease" contains mathematics methods that calculate the values. No need to understand it, just have to know how to use it. The result of all of this could be seen here.

Of course the class is not fully functional. I mean there are no checks if some of the properties like "start" or "end" are missing. We can also add an ability to call a callback method when some of the tweens finish. It's just a basic manager that you can develop.
In some of the next articles I will show you how to use this class together with Papervision3D to animate object in the 3D space.
Sharing ...
Commenting ...
blog comments powered by Disqus