Panning Sounds

[ LiB ]

What does it mean to pan a sound? It simply means to control the left and right speakers independently by individually adjusting their volume levels. This allows you to make really cool effectsfor example, if you ever create a game with explosives, such as a war game, you can have the explosions near the right side of the screen sound at a higher volume from the right speaker than the left. Same thing with the explosions towards the left side of the screenyou can make the sound at a higher volume from the left speaker than the right. This, of course, will help your player disappear into your game. This is the essence of stereo soundssee Figure 10.11.

Figure 10.11. The effects of using stereo sounds


I modified the previous program so that instead of the slider adjusting the volume, you have a slider that adjusts the panning from the left and right speakers. It's pretty cool, if you ask me. Open the demo GDA_PROG10.5.fla and review the code. I have posted it here as well. I didn't post the code that's on the main Timeline, because all it does is create a Sound object, attach the sound from the library, and then loop the object for a long time.

 // The onClipEvent(load) handler // initializes all other handlers // in this Movie Clip. onClipEvent(load) {   // These properties   // are used and can be   // adjusted to where you   // want your slider min   // and max to be on the   // x axis.   this.minX = 100;   this.maxX = 300;   // Define another sound   // object to control the   // sound globally--this is   // why it's declared on _root. = new Sound();   // This handler is declared   // and executed for when the   // mouse clicks on the mc--   // it then flags its variable   this.onPress = function() { = true;   };   // This flags the rest of the   // handlers that the mouse was   // released.   this.onRelease = function() { = false;   };   // This function was also declared   // for when the user releases the   // mouse outside of the clip.   this.onReleaseOutside = function() { = false; };  // This is the heart of it all-- // It is executed when the mouse is // moved and if pressed. this.onMouseMove = function() {       // This only executes if the mouse       // is being pressed (and flagged)   if ( {       // Follow the _xmouse to       // simulate dragging.       this._x = _root._xmouse;       // Constrain this position to       // the max property that we set.       if (this._x > this.maxX) {       this._x = this.maxX;     }       // Also constrain our position       // to the min property we set.       if (this._x < this.minX) {       this._x = this.minX;     }     // Set the pan of our music object depending on how far along     // the slider our mc is. See the text for an explanation of the formula.*200)-100));     }   }; } 

As you can already tell, most of the code is the same. Just to recap, I'll briefly explain the sections.

All of the event handlers used are declared in the load event handler of the Movie Clip slider.

Properties were created to keep track of the minimum and maximum positions that the slider should be within.

A global music object was created to adjust the panning to all sounds within the Flash project. I created the music object this way to avoid any confusion. If you would like the sound controls to affect a certain Movie Clip's sounds, then declare the Sound object within that Movie Clip.

The onPress, onRelease, and onRleaseOutside handlers have all been used to flag the rest of the program when the user presses or releases the mouse button.

Even most of the juicy stuff within the onMouseMove has been retained from the last demo. All of the if structures that restrict movement and the slider positioning code do the same as in the last example. The main difference in this code is the last command, the setPan method of the Sound object.*200)-100)); 

The setPan method accepts values from 100 (meaning the left speaker only produces sound) to 100 (where sound only comes out of the right speaker), with 0 being dead center. Of course, when 0 is passed on to the setPan method, sound comes out of both speakers equally. Anything in between these main values causes one speaker to produce more sound than the other, allowing your ears to pick up one side more prominently than the other. This is the beauty of stereo sound.

I'll show you how I used the values of this Movie Clip to determine the value to pass on to setPan . Just like in the last example, I calculated how far along the slider I was. I then divided this by how long the slider actually is. I then multiplied this value by 200 so I could have enough space to subtract another number that can yield a range from 100 to 100. This is why I then subtracted 100 from that value.

Let's say this slider bar was 200 pixels long. Our slider is positioned at 50. When dividing 50 by 200 we get 0.25. When this value is multiplied by 200, it yields 50. When 100 is subtracted from this, you get 50. This will cause the pan to steer the sounds to the left speaker and leave only some sound on the right.

[ LiB ]

Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta © 2008-2017.
If you may any questions please contact us: