Transforming Sounds

[ LiB ]

Flash allows you to create some very interesting effects with your sounds. A setTransform method exists that accepts a generic object as its only parameter. This method allows you to play some of the right channel information in your left speaker and some of your left channel information on your right speakerheck, you can even flip the channels.

Using this technique you can program the sound so that when, in your action-adventure game, your character enters a cave, fires at a target, and misses, the bullet's "ricochet" can be heard . The cool thing about this is that you can have your program select what speakers and volume percentages output the sound depending on where (on the screen) your player's projectile ricocheted from.

I have written up demo GDA_PROG10.6.fla to demonstrate some of the things I'll discuss in this section. When you test the movie, it makes a sound that says "left" in the left speaker and then moves on to say "right" in the right speaker.

NOTE

TIP

A stereo sound will be converted to mono if you use Flash's default set tings. Adjust the settings by right- clicking (or Control-clicking for Macs) the sound on the Library window and selecting Raw from Export Settings then unchecking Convert to Mono.

When the Channel Swap button is clicked, "left" is spoken out the right speaker and "right" is spoken out of the left speaker. You can return this to normal by clicking the Reset button. How does this work? Check out the following listing.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // In this demo you are shown // how to create a sound object // so you can control sounds // from within ActionScript. // Create the new Sound object mySound = new Sound(); // Attach a sound to play from the library mySound.attachSound("LeftRight"); // Play the sound starting at 0 pos // looping for 9,999 times. mySound.start(0, 9999); // Define the transform objects ChannelSwap = { ll: 0, rr: 0, lr: 100, rl: 100}; Reset = { ll: 100, rr: 100, lr: 0, rl: 0}; 

The generic object that is passed on to setTransform is an object with the following properties:

 MyObject.ll MyObject.rr MyObject.lr MyObject.rl 

These properties can contain values that range from 0 to 100. The ll and rr properties act like individual volumes for the left and right speakers, respectively. In other words, when ll is 50 and rr is 100, this would mean that the left speaker is at 50 percent and the right speaker is at 100 percent.

The lr and rl properties represent how much leakage there is from one speaker to the other. The lr represents how much of the right channel comes out of the left speaker, and rl represents how much of the left channel comes out of the right speaker.

What if you want to reverse the channels? Use these settings:

 MyObject.ll = 0; MyObject.rr = 0; MyObject.lr = 100; MyObject.rl = 100; 

If MyObject were to be passed onto setTransform , it would mute the ll and rr channels while blasting whatever is on the right channel through the left speaker and whatever is on the left channel through the right speaker.

So back to the listing. Besides declaring, attaching, and playing the sound from the Sound object, I also declared two generic objects with the properties described above:

 ChannelSwap = { ll: 0, rr: 0, lr: 100, rl: 100}; Reset = { ll: 100, rr: 100, lr: 0, rl: 0}; 

The object ChannelSwap reverses the channels just as I explained. It doesn't do so by itself, thoughthe object has to be passed on to setTransform . You'll see how soon.

The Reset object actually mutes the channel swapping and sets the left and right channels back up to 100 percent.

Let's check out the button codethis is where the channel transformations are taking place.

Look at the actions in the Channel Swap button. You should see something like the following:

 on (release) {   mySound.setTransform(ChannelSwap); } 

Once this command is called with the object, the channels are adjusted immediately. I'm sure you already have a good idea of what the Reset button does:

 on (release) {   mySound.setTransform(Reset); } 

Just as you guessed, it passes on the Reset object to the setTransform method of the mySound object. Everything goes back to normal after the execution of this command. How nice.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net