Sound.setPan( ) Method | Flash 5 |
set the balance of a sound's left and right channels |
A number between -100 (left) and 100 (right), indicating the distribution between the left and right speakers for sounds controlled by soundObject. If the pan supplied is greater than 100, the actual value assigned is 200 - pan. If the pan supplied is less than -100, the actual value assigned is -200 - pan.
The setPan( ) method dictates the balance of the right and left channels of the sounds controlled by soundObject. By adjusting the pan over time, we can cause a sound to move from one speaker to the other (known as panning).
To play the sounds controlled by soundObject in the left speaker only, use a pan of -100. To play the sounds controlled by soundObject in the right speaker only, use a pan of 100. To balance the two channels evenly, use a pan of 0.
Note that setPan( ) affects all the sounds controlled by soundObject. If soundObject is a Player-wide sound, setPan( ) affects all the sounds in a movie. If soundObject is tied to a clip or a main timeline, setPan( ) affects all the sounds in that clip or timeline and all the clips it contains.
The effects of setPan( ) can be changed only by another call to setPan( ). A setPan( ) assignment affects all future sounds controlled by soundObject, even if soundObject is deleted.
The following code adds a new method, doPanEffect( ), to all movie clips, which causes sounds in a movie clip to pan endlessly between the left and right speakers:
// Method: MovieClip.doPanEffect() MovieClip.prototype.doPanEffect = function () { var panEffect = new Sound(this); var panDirection = "right"; var panIncrement = 10; this.onEnterFrame = function () { if (panDirection = = "right") { newPan = panEffect.getPan() + panIncrement; if (newPan > 100) { panDirection = "left"; panEffect.setPan(panEffect.getPan() - panIncrement); } else { panEffect.setPan(newPan); } } else { newPan = panEffect.getPan() - panIncrement; if (newPan < -100) { panDirection = "right"; panEffect.setPan(panEffect.getPan() + panIncrement); } else { panEffect.setPan(newPan); } } } } // Usage: theSoundClip_mc.doPanEffect();
The following example causes sounds in soundClip_mc to react to the mouse. Assuming a Stage width and height of 550 and 400, the sounds pan left and right with the mouse's horizontal position and increase or decrease in volume with the mouse's vertical position. To try out the effect, create a new movie clip instance named soundClip_mc on the main timeline and attach the following code to frame 1 of the main timeline. Then either place sounds inside soundClip_mc or export a sound from the Library with an export identifier of "bgMusic".
// Create a new Sound object and attach the sound bgMusic to it soundClip_mc.volControl = new Sound(soundClip_mc); soundClip_mc.volControl.attachSound("bgMusic"); soundClip_mc.volControl.start(0, 999); // Play and loop the sound // With every frame, set volume and pan soundClip_mc.onEnterFrame = function () { // Measure the mouse's horizontal location, then set the pan accordingly var mouseX = (_root._xmouse / 550) * 200; this.volControl.setPan(mouseX - 100); // Measure the mouse's vertical location, then set the volume accordingly var mouseY = (_root._ymouse / 400) * 300; this.volControl.setVolume(300 - mouseY); }
Sound.getPan( ), Sound.setTransform( ), Sound.setVolume( )