« Home « Kết quả tìm kiếm

Controlling Panning


Tóm tắt Xem thử

- Similar to setting the volume of a Sound object, setting a Sound object's panning is straightforward, as the following example demonstrates:.
- This code causes the bounce sound to play out of the right speaker only.
- You can set a Sound object's panning anywhere between -100 (left speaker only) and 100 (right speaker only), with a setting of 0 causing the sound to play equally out of both speakers..
- As with the setVolume() method, the setPan() method can use the value of a variable to set a Sound object's panning more dynamically, as the following example demonstrates:.
- We'll use a variable to set the pan of the bounce Sound object.
- As in the preceding exercise, this variable will contain a percentage value between -100 and 100.
- We'll base the pan setting of the bounce Sound object on the horizontal distance of the mouse pointer from the center point in either the left or right quadrant..
- Determine the horizontal size of the draggable boundary and then split it in two, essentially breaking the boundary into two "quadrants,".
- Establish the position of the horizontal center..
- Determine the mouse pointer's current horizontal position (at the exact center or in the left or right quadrant) each time it's moved..
- If the mouse pointer is at the exact center point, the pan is set to 0.
- If the mouse is left of the center point (in the left quadrant), the pan is set to a value between -100 and 0.
- This value represents the horizontal distance (percentage-based) of the mouse pointer from the center point in relation to the overall size of the quadrant.
- Likewise, if the mouse pointer is right of the center point (in the right quadrant), the pan is set to a value between 0 and 100, which represents the horizontal distance (percentage-based) of the mouse pointer from the center point in relation to the overall size of the quadrant..
- We've already discussed most of the principles for translating this logic into ActionScript.
- Continue using the file you were working with at the end of the preceding exercise..
- With the Actions panel open, select Frame 1 of the Actions layer.
- After the line of script var boundaryHeight:Number = bottomBoundary - topBoundary, insert the following line of script:.
- This line creates a variable named boundaryWidth and assigns it the value of rightBoundary - leftBoundary.
- The lines of script directly above this one indicate that rightBoundary = 490 and leftBoundary = 60.
- This is how the line of script looks when written out:.
- This value represents the horizontal size of the boundary and will be used to determine the size of the left and right quadrants of the boundary..
- Add the following line of script after the line added in Step 2:.
- This step creates a variable named quadrantSize and assigns it a value based on the value of boundaryWidth / 2.
- At this point, boundaryWidth has a value of 430.
- This line of script written out would look like this:.
- You need to know the size of these quadrants to determine what percentage values to use for setting the pan of the bounce Sound object..
- Add the following line of script after the line added in Step 3:.
- This step creates a variable named centerPoint and assigns it the value of.
- At this point, rightBoundary has a value of 490 and quadrantSize has a value of 215.
- Here's how this line of script would look when written out:.
- This value denotes the horizontal location of the center point of the boundary—the place where the left and right quadrants meet.
- This variable plays a critical role in the panning process because it allows us to determine which quadrant the mouse pointer is in—and thus whether the bounce Sound object should be panned left or right.
- If the mouse pointer's horizontal position (_xmouse) is greater than.
- centerPoint (275), we know that the mouse pointer is in the right quadrant.
- if it's less than 275, we know the mouse pointer is in the left quadrant..
- in the script (within the onMouseEvent handler), insert the following lines of script:.
- 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 object.
- 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.
- plugging in the values for centerPoint (275) and quadrantSize (215), we can break down this expression in the following way:.
- After the value of panAmount has been determined, the next line of script sets the pan of the bounce Sound object based on the value that the expression assigned to.
- 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 to the right of the center point..
- Assume that the mouse pointer's horizontal position is 158.
- This is the result of subtracting 275 from 158 at the beginning of the expression.
- –117 (a negative number), the expression evaluates to a negative value—ideal 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 object 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 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..
- In the testing environment, move your mouse pointer around the court.
- As you drag the ball, not only does the bounce's volume change, so does the location of the sound—moving from left to right and vice versa.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt