Tuesday, 22 September 2009

bouncing ball

function BlueBall() {
startFallingBall();

setupResetFunctionality();
}

function startFallingBall() {
timer=0;
initialPos=this.y;
this.addEventListener(Event.ENTER_FRAME, moveBallDown);
}
// Responsible for moving the ball down
function moveBallDown(e:Event) {
timer+=1;
this.y = initialPos + .5*gravity*(timer * timer);
checkBottomBoundary();
}
this.y = initialPos + .5*gravity*(timer * timer);
function checkBottomBoundary() {
if (this.y+this.height>stage.stageHeight) {
finalPos=this.y;

stopFallingBall();
}
}
This method checks to see if
function stopFallingBall() {
this.removeEventListener(Event.ENTER_FRAME, moveBallDown);
// If the bounce is a 10% of the ball's height, just stop
// the bounce
if (finalPos-initialPos<.1*this.height) {
stopRisingBall();
} else {
startRisingBall();
}
}

if (finalPos-initialPos<.1*this.height) {
stopRisingBall();
} else {
startRisingBall();
}

// Sets up what is needed to start bouncing the ball up
function startRisingBall() {
initialSpeed=decay*Math.sqrt(2*Math.abs(finalPos-initialPos));
timer=0;
currentPos=this.y;
this.addEventListener(Event.ENTER_FRAME, moveBallUp);
}

initialSpeed=decay*Math.sqrt(2*Math.abs(finalPos-initialPos));

// Responsible for moving the ball up
function moveBallUp(e:Event) {
timer+=1;

//Storing the position of the ball before and after it moves
var positionA:Number=this.y;
this.y = currentPos - initialSpeed*timer + .5*gravity*(timer * timer);
var positionB=this.y;

checkTopBoundary(positionA, positionB);
}
this.y = currentPos - initialSpeed*timer + .5*gravity*(timer * timer);

// Checks when the ball has hit the top of the bounce
function checkTopBoundary(firstPos:Number, secondPos:Number) {
if (secondPos>firstPos) {
stopRisingBall();
startFallingBall();
}
}

var timer:Number=0;
var initialPos:Number=0;
var finalPos:Number=0;
var currentPos:Number=0;
var initialSpeed:Number=0;
var startPosition:Number=0;
var gravity:Number = 1;

//Adjust this to increase or decrease the
//number of bounces
var decay:Number = .9;
}

No comments: