The Pinball Wizard

Josh Schoenwald
accad 756
task 09: If, then, else
11/05/02




//
// Josh Schoenwald *** accad 756 *** task 09 *** 11/05/02 
//

// function does all the work
function pinballWizard(allCircles, x, y, velocityX, velocityY) {

	for (i=0; i<6; i++) {  //this loop scales all the pinballs to the size I want
		allCircles[i]._xScale = 150;
		allCircles[i]._yScale = 150;
	}

	for (i=0; i<3; i++) {
		if (allCircles[i]._x>=(Stage.width-allCircles[i]._width)) {
			velocityX[i] = -velocityX[i];
			if ((allCircles[i]._y>=(Stage.height-allCircles[i]._width)) || (allcircles[i]._y<=allCircles[i]._width)) {
				velocityY[i] = -velocityY[i];
			}
		} else if (allCircles[i]._x<=allCircles[i]._width) {
			velocityX[i] = -velocityX[i];
			if ((allCircles[i]._y>=(Stage.height-allCircles[i]._width)) || (allcircles[i]._y<=allCircles[i]._width)) {
				velocityY[i] = -velocityY[i];
			}
		} else if (allCircles[i]._y>=(Stage.height-allCircles[i]._width)) {
			velocityY[i] = -velocityY[i];
			if ((allCircles[i]._x>=(Stage.width-allCircles[i]._width)) || (allCircles[i]._x<=allCircles[i]._width)) {
				velocityX[i] = -velocityX[i];
			}
		} else if (allcircles[i]._y<=allCircles[i]._width) {
			velocityY[i] = -velocityY[i];
			if ((allCircles[i]._x>=(Stage.width-allCircles[i]._width)) || (allCircles[i]._x<=allCircles[i]._width)) {
				velocityX[i] = -velocityX[i];
			}
		}
		
		/**************************************************************************************************
		The above set of commends performs a number of checks. The 'for' loop is set to run through all 3 circles.
		Then it checks to see if the ball is at the left edge. If it is, it reverses the x-velocity and then
		checks to see if it's in a corner by checking the y-values. If it is in a corner, the y-velocity is ALSO
		reversed, which keeps it from sneaking off the canvas. (it does this if it hits a corner dead-on)
		If it was NOT at the left edge, it checks the RIGHT edge and performs the same functions.  If the
		ball in question is not at EITHER edge, it checks to see if it's at the BOTTOM of the screen by 
		checking y-values.  If it IS near the bottom, it reverses the y-velocity and then checks the x-values to 
		see if it's in a corner. If it is, then it ALSO reverses the x-velocity, so it doesn't sneak off the
		canvas.  If the ball is NOT at the bottom of the canvas, it performs the same functions.  if it's
		not near any edge, it continues with the next 2 lines below.
		***************************************************************************************************/
		
		
		allCircles[i]._x += velocityX[i];  //these lines actually assign the new velocity and update the position
		allCircles[i]._y += velocityY[i];
	}
	
	
	if (distance(allCircles[0], allCircles[1])<=allCircles[0]._width) {
		velocityX[0] = -velocityX[0];  
		velocityY[0] = -velocityY[0];  //this set checks to see if ball 1 is near ball 2
		velocityX[1] = -velocityX[1];  //if so, both balls' velocities are reversed
		velocityY[1] = -velocityY[1];
	}
	if (distance(allCircles[0], allCircles[2])<=allCircles[0]._width) {
		velocityX[0] = -velocityX[0];
		velocityY[0] = -velocityY[0];  //this set checks to see if ball 1 is near ball 3
		velocityX[2] = -velocityX[2];  //if so, both balls' velocities are reversed
		velocityY[2] = -velocityY[2];
	}
	if (distance(allCircles[1], allCircles[2])<=allCircles[1]._width) {
		velocityX[1] = -velocityX[1];
		velocityY[1] = -velocityY[1];  //this set checks to see if ball 2 is near ball 3
		velocityX[2] = -velocityX[2];  //if so, both balls' velocities are reversed
		velocityY[2] = -velocityY[2];
	}
	for (i=0; i<3; i++) {   //this loop goes 3 times, one pass for each pinball
		for (j=3; j<6; j++) {  //this loop goes 3 times also, once for each bumper
			if (distance(allCircles[i], allCircles[j])<=allCircles[i]._width) {
				velocityX[i] = -velocityX[i];  //this reverses the velocity of the ball if it hits a bumper
				velocityY[i] = -velocityY[i];
			}
		}
	}
}


function distance(ball1, ball2) {  //returns the distance between the 2 balls (or a ball and a bumpber)
	return Math.sqrt(Math.pow((ball2._x-ball1._x), 2)+Math.pow((ball2._y-ball1._y), 2)); 
	//standard distance formula: D = squareroot of: (square of: (x2 - x1) + square of: (y2 - y1))
}


//circles I am using are named here
var circles = new Array(circle01, circle02, circle03,
								circle04, circle05, circle06,
								circle07, circle08, circle09);

var x = 0; //variables for moving pinballs
var y = 0;

var velocityX = new Array(19, 15, 16);  //defines velocity and also direction of travel
var velocityY = new Array(8, 18, 12);  //velocites are intentionally different for variety

function updateFrame() {
	pinballWizard(circles, x, y, velocityX, velocityY); //calls the main function
	x++; //increments x and y values to continue animation
	y++;
}

// associate a function with the enterFrame event
this.onEnterFrame = updateFrame;


//
// Josh Schoenwald *** accad 756 *** task 09 *** 11/05/02 
//