Tuesday, 22 September 2009

gravity in action script

hre i have written a simple code to show how gravity can me applied to any object in flash.

// create your variables for use later
var gravity:Number = 2;
var velocity:Number = 0;


These are two defined variables - gravity and velocity. Gravity is a constant downward acceleration, set to 2. Velocity is the current vertical speed of the object in question.


enter the following


if ( this._y >= 453 and _root.velocity < 2.5 and _root.velocity >= 0 ) { this._y = 460; _root.velocity =0; }
else {
if ( this._y >= 460 and _root.velocity>0 ) { _root.velocity = -_root.velocity * .7 ; }
this._y += _root.velocity;
_root.velocity += _root.gravity;
}

No comments: