Ball Toss Game

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Grab the ball and toss it around! Points are tallied one per each bounce off the wall. Hint: throw the ball diagonally towards any corner.


//Border variables. Right and Bottom are subtracted to compensate for balls top left registration point
var xBounds:Object = {left:17 , right:383 - ball.width};
var yBounds:Object = {top:13 , bottom:287 - ball.height};

//The current and last x and y position of the ball. Used to calculate velocity
//initially set to balls x and y to prevent the peskiest glitch I encountered this project
var currentX:Number = ball.x;
var currentY:Number = ball.y;
var lastX:Number = ball.x;
var lastY:Number = ball.y;

// Velocity x and y properties
var vx:Number = 0;
var vy:Number = 0;

//Offset variable used to prevent the ball from snapping to mouse
var xOffset:Number;
var yOffset:Number;

//Variables for the best and current score. 
var bestScore:Number = 0;
var currScore:Number = 0;

// friction is used to slowly stop the ball. A form of easing
var friction = 0.95;
/* dead speed refers to a speed so slow, its practically zero. this is used to prevent the enter frame from constantly dividing extremely small numbers. it is also used to indicate when the round is over*/
var deadSpeed:Number = 0.01;

// ballThrown is a control variable used mainly for the onUp function
var ballThrown:Boolean = false;

//set up the text fields
bestS.text = "BEST: " + bestScore;
currS.text = "CURRENT: " + currScore;

//animated filter
var bff:BlurFilter = new BlurFilter();
//low quality works better
bff.quality = 1;

// --Listeners------------------------------

stage.addEventListener(Event.ENTER_FRAME, onFrame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
//mouse up added to stage to prevent issues when throwing the ball
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
cursor.addEventListener(Event.ENTER_FRAME, cursorControl);
/*ball.addEventListener(Event.ENTER_FRAME, gravity);

function gravity(e:Event):void{
	if(ball.y < yBounds.bottom-3){
		ball.y += ball.y/30;}else{ball.y = yBounds.bottom -2;}

function onFrame(e:Event):void{
	//if both velocities are between .05 AND -.05 then the round is over
	if(vx < deadSpeed && vy < deadSpeed && 
	    vx > -deadSpeed && vy > -deadSpeed){

	ball.x += vx;
	ball.y += vy;
	vx *= friction;
	vy *= friction;
	if(ball.x >= xBounds.right){ball.x = xBounds.right-1; xWallHit();}
	if(ball.x <= xBounds.left){ball.x = xBounds.left+1; xWallHit(); }
	if(ball.y >= yBounds.bottom){ball.y = yBounds.bottom-2; yWallHit();}
	if(ball.y <={ball.y =; yWallHit();}
	bff.blurX = vx;
	bff.blurY = vy;
	ball.filters = [bff];

function onDown(e:MouseEvent):void{
	//a control statement practically adding an event listener to the ball
	//the offset is equal to the mouse's coordinate in relation to the ball's registration
	xOffset = ball.mouseX/2;
	yOffset = ball.mouseY/2;
	//if the ball is clicked on, it being dragged, NOT thrown
	ballThrown = false;
	//change the cursor graphic to a grabbing hand
	//when ball is clicked, reset score
	//when the ball is clicked, run the onDownFrame and remove onFrame
	removeEventListener(Event.ENTER_FRAME, onFrame);
	addEventListener(Event.ENTER_FRAME, onDownFrame);

function onDownFrame(e:Event):void{
	ball.x = mouseX - xOffset;
	ball.y = mouseY - yOffset;
	lastX = currentX;
	lastY = currentY;
	currentX = mouseX;
	currentY = mouseY;
	if(ball.x > xBounds.right){ball.x = xBounds.right-1;}
	if(ball.x < xBounds.left){ball.x = xBounds.left+1;}
	if(ball.y > yBounds.bottom){ball.y = yBounds.bottom-1;}
	if(ball.y <{ball.y =;}

function onUp(e:Event):void{
	//control statement used to disable onUp function after ball is thrown
	if(ballThrown == false){
		//velocity is a difference between the balls current position and last.
		//the bigger the difference, the faster the ball will go.
		vx = currentX - lastX;
		vy = currentY - lastY;
		//reset current and last x/y to zero to prevent velocity calculation issues
		currentX = 0; currentY = 0;
		lastX = 0; lastY = 0;
		//since mouse is released, ball is being thrown
		ballThrown = true;
		//when mouseUp, remove the onDownFrame and run the onFrame
		removeEventListener(Event.ENTER_FRAME, onDownFrame);
		addEventListener(Event.ENTER_FRAME, onFrame);

function cursorControl(e:Event):void{
	cursor.x = mouseX;
	cursor.y = mouseY;

// --Functions-----------------------------

//when the ball hits a wall, increment score by one, and reverse the velocity
function xWallHit():void{
	vx *= -1;
function yWallHit():void{
	vy *= -1;

//when the round is over, set velocities to zero, update scores, remove enter frame
function roundOver():void{
	vx = 0;
	vy = 0;
	removeEventListener(Event.ENTER_FRAME, onFrame);
//simple high score updater
function updateBestScore():void{
	if(currScore > bestScore){
		bestScore = currScore;
		bestS.text = "BEST:" + bestScore;

function updateCurrScore():void{
	currScore += 1;
	currS.text = "CURRENT:" + currScore;
function resetCurrScore():void{
	currScore = 0;
	currS.text = "CURRENT:" + currScore;