Macromedia Flash 8 ActionScript: Training from the Source
Although the volume of a sound gives a sense of distance, panning helps determine its left/right position. Similar to setting the volume of a Sound instance, setting a Sound instance's panning is straightforward, as the following example demonstrates: bounce.setPan (100);
This code causes the bounce sound to play out of the right speaker only. You can set a Sound instance'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 instance's panning more dynamically, as the following example demonstrates: bounce.setPan (myVariable);
We'll use a variable to set the pan of the bounce Sound instance. As in the preceding exercise, this variable will contain a percentage value between -100 and 100 (encompassing the entire spectrum of panning values). We'll base the pan setting of the bounce Sound instance on the horizontal distance of the mouse pointer from the center point in either the left or right section. To make this technique work, we must do the following:
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 section), 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 section. Likewise, if the mouse pointer is right of the center point (in the right section), 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 section. Don't worry if you're confused. We've already discussed most of the principles for translating this logic into ActionScript; we just need to adapt them a bit.
|