Simple example of acceleration

While teaching my Actionscript Fundamentals course a couple of days ago one of the trainees asked me how to do a simple example of using acceleration and deceleration within an animation. Here it is. Roll over the centre of the propeller below.

The code looks like this.

var rate:Number = 0;
var acceleration:Number = 0.5;

prop_mc.centre_mc.onRollOver = function()
{
    prop_mc.onEnterFrame = function()
    {
        rate += acceleration;
        rate = Math.min( rate, 15 );
        prop_mc._rotation += rate;
    }
};

prop_mc.centre_mc.onRollOut = function()
{
    prop_mc.onEnterFrame = function()
    {
        rate -= acceleration;
        rate = Math.max( rate, 0 );
        prop_mc._rotation += rate;
    }
};

prop_mc.centre_mc.onDragOver = prop_mc.centre_mc.onRollOver;
prop_mc.centre_mc.onDragOut = prop_mc.centre_mc.onRollOut;

For a brief explaination, rate is the speed of rotation of the propeller. When the mouse is over the propeller, the rate increases each frame by the acceleration and is capped at a maximum rate of 15. When the mouse is not over the propeller, the rate decreases each frame by the acceleration and is capped at zero. The result is what you see.

The source file can be downloaded here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>