Flash actionscript 3.0: gravity force in AS3.0

gravity force in AS3.0

Posted by Sankar.G | Posted in | Posted on 11:45 AM

stop();
var speedx:Number=0;
var speedy:Number=0;
var fri:Number=0.98765;
var radius:Number=ball_mc.height/4;
var ax:Number=0;
var ay:Number=0;
var gravity:Number=2.5;
addEventListener(Event.ENTER_FRAME,pull);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyup);
function pull(e:Event) {

speedy+=gravity;
speedx+=ax;
speedy+=ay;
speedy*=fri;
speedx*=fri;
ball_mc.y+=speedy;
ball_mc.x+=speedx;
if (ball_mc.y+radius>stage.stageHeight) {
ball_mc.y=stage.stageHeight-radius;
speedy*=-1;
} else if (ball_mc.y-radius<0) { ball_mc.y=radius; speedy*=-1; } else if (ball_mc.x>stage.stageWidth)
{
ball_mc.x=stage.stageWidth-radius;
speedx*=-1;
}
else if(ball_mc.x-radius<0) { ball_mc.x=radius; speedx*=-1; } } function keydown(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.LEFT : ax = -1; break; case Keyboard.RIGHT : ax = 1; break; case Keyboard.UP : gravity = 0; ay = -0.5; break; case Keyboard.DOWN : ay = 1; break; } } function keyup(e:KeyboardEvent):void { gravity = 1; ax = 0; ay = 0; } Examples: Gravity in AS3.swf

Comments (0)