Wednesday 28 November 2012

How to Bouncing or falling a ball in action script 3.0



var ball:Sprite = new Sprite();
ball.graphics.beginFill(0);
ball.graphics.drawCircle(0, 0, 10);
ball.x = 275;
ball.y = 390;
addChild(ball);

var xPos:Number = ball.x;
var yPos:Number = ball.y;
var xVel:Number = (Math.random() * 10) - 5;
var yVel:Number = (Math.random() * -10) - 10;
var grav:Number = 1;

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void
{
    yVel +=  grav;
    xPos +=  xVel;
    yPos +=  yVel;

    if (yPos > 390)
    {
        yPos = 390;
        yVel *=  -.7;
        xVel *=  .7;
    }

    ball.x = xPos;
    ball.y = yPos;
}

No comments:

Post a Comment