After the line basketball_mc._yscale = topToBottomPercent; in the script (within the onMouseEvent handler), insert the following lines of script:
var panAmount = ((_xmouse - centerPoint) / sectionSize) * 100;
bounce.setPan(panAmount);

The variable
panAmount is created in the first line. The expression that sets the value of
panAmount is based on the percentage-generating equation we used to set the volume. After the value of
panAmount has been established, this variable is used to set the pan for the
bounce Sound instance. The expression is set up to generate a value between
100 and
-100 for
panAmount.
To help you understand how this section of script works, we'll look at a couple of scenarios. Let's assume that the mouse pointer's horizontal position (
_xmouse) is 374 when the expression that sets the value of
panAmount is evaluated. By plugging in the values for
centerPoint (275) and
sectionSize (215), we can break down this expression in the following way:
panAmount = ((374 275) / 215) * 100
or
panAmount = (99 / 215) * 100
or
panAmount = .4604 * 100
or
panAmount = 46.04
After the value of
panAmount has been determined, the next line of script sets the pan of the
bounce Sound instance based on the value that the expression assigned to the
panAmount variable. At this point, the value of
panAmount is
46.04. Setting the pan to this amount will cause it to sound 46.04 percent louder in the right speaker than in the left, indicating that the ball is on the right side of the basketball court. Visually, the ball will appear on the right side of the court as well because the mouse pointer's horizontal position (374) is greater than that of
centerPoint (275), indicating that the mouse pointer (and thus the
basketball_mc movie clip instance) is 99 pixels (374 275) to the right of the center point.
Now let's look at another scenario. Assume that the mouse pointer's horizontal position is 158. Plugging all the necessary values into our expression, we can break it down as follows:
panAmount = ((158 275) / 215) * 100
or
panAmount = (117 / 215) * 100
or
panAmount = .5442 * 100
or
panAmount = 54.42
In this scenario,
panAmount is set to a negative number (54.42). This is the result of subtracting 275 from 158 at the beginning of the expression. Because 158 275 = 117 (a negative number), the expression evaluates to a negative valueideal because we need a negative value to pan our sound to the left. After the value of
panAmount has been determined, the next line of script sets the pan of the
bounce Sound instance based on the value that the expression assigned to the
panAmount variable (54.42). This causes the bounce to sound 54.42 percent louder in the left speaker than in the right, indicating that the ball is on the left side of the basketball court. Visually, the ball will appear on the left side of the court as well because the mouse pointer's horizontal position (158) is less than that of
centerPoint (275), indicating that the mouse pointer (and thus the
basketball_mc movie clip instance) is 117 pixels (158 275) to the left of the center point.
If the mouse pointer's horizontal position is equal to that of
centerPoint (275), the expression sets the value of
panAmount to
0, causing sound to come out equally from the left and right speakers. This in turn indicates that the ball is in the center of the court.
Because the two lines of script in this step are nested within the
onMouseMove event handler as well as in the
if statement that looks for movement within the boundary, the sound is panned each time the mouse pointer is moved within the boundary.