Quantcast
Viewing latest article 1
Browse Latest Browse All 4

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.

Flash required: You need version 10 or later of the free Flash player from Adobe to use this content. To download and install the free player from Adobe's web site click here.

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.


Viewing latest article 1
Browse Latest Browse All 4

Trending Articles