Quantunet.com

My Account

Joins Us
Flash 8 Actionscript 2.0 Knowledgebase
Simple Classic Ease  

Download

How to create a simple classic ease tween.

Actionscript

var easing:Number = 0.2;
var target_x:Number = Stage.width/2;

ball_mc._x = 10;

ball_mc.onEnterFrame = function(){
ball_mc._x += (target_x - ball_mc._x)*easing;
};



Note: Press the "refresh" button in the browser to play the animation again.


All the actionscript is on the first frame of the main timeline.
First the variables are "easing" and "target_x" are declared, defined as numbers and then assigned values.

var easing:Number = 0.2;
var target_x:Number = Stage.width/2;

ball_mc._x = 10;

A function is created to act on the movie clip "ball_mc" .

ball_mc.onEnterFrame = function(){
ball_mc._x += (target_x - ball_mc._x)*easing;
};

The event handler "onEnterFrame" calls the function every time a new frame is entered. When the function is called it finds the difference between the present x position of the movie clip "ball_mc" and the target position "target_x",

target_x - ball_mc._x

multiplies the difference by the easing factor,

(target_x - ball_mc._x)*easing;

then adds the result to the present x coordinate value of the movie clip "ball_mc._x".

ball_mc._x += (target_x - ball_mc._x)*easing;

This reevaluation of the position of ball_mc is done every time a frame is entered. That is 24 times a second the position of the movie clip is calculated and positioned.

With an easing factor that is smaller than one the value that is added to the horizontal position of the movie clip decreases each time the function is called. The net effect is to make the ball appear to decelerate as it approaches the target value.

For example: If the value of ease is set to 0.5 the amount added to the x coordinate will be half the separation distance.

var easing:Number = 0.5;
var target_x:Number = 300;

ball_mc._x = 0;

ball_mc.onEnterFrame = function(){
ball_mc._x += (target_x - ball_mc._x)*easing;
};

  • The first time the function is called it will calculate a a difference of 300 - 0 as the ball_mc is at the point 0 and has a target point of 300. The function then multiples this difference by 0.5 giving 150. This is then set as the new position of the ball_mc.
  • The function is called again and calculates a difference of 300-150 = 150 multiples by the easing factor 0.5 to give 75 then add this to the x coordinate making a new position of 225.
  • When the function is called again the difference is 300-225 = 75 when multiplied by the easing factor it becomes 37.5 and the new x coordinate value becomes 225 + 37.5 = 262.5.

This process continues as the function is repeatedly called and the total gets closer and closer to 300.

150,
225,
262.5,
281.25,
290.6,
295.3,
297.65,
298.8,
299.4,
299.7,
299.85,
299.9,
299.95,
299.95,
299.95,
299.95,
299.95,
299.95,
299.95,
299.95,
299.95,
299.95, and so on.....

The final position of the movie clip is never quite 300. Mathematically if you were to have a perfectly accurate result each time it would take an infinite number of steps to reach the number 300. But you would in theory arrive at the number 300 exactly.

In Flash however there is a limit to the precision of the calculated values and so the value never exceeds 299.95.
But the since the function is called every time it enters a frame it continues to calculate and reposition the mc for as long as the user continues to view the swf.

Note: Right click on the above diagram to zoom in a see the detail around the target line.

You may recognize this as Zeno's dichotomy paradox or the actionscript model a variation of a zeno machine.

In effect this easing function creates an exponential acceleration towards the target point. Although this is an elegant way to create an exponential ease it lacks flexibilty. Exponential easing can also be achieved using the exp math class which makes a more versatile easing script. [How to create custom exponential easing]

© 2008 Quantunet LLC All Rights Reserved | Intellectual Property | Terms of Use | Privacy
Home | About Quantunet | FAQ's | Contact Us