Attaching Sounds Dynamically

[ LiB ]

In ActionScript, you can load and link sounds from your library and play them whenever you wish. All you have to do is create a Sound object that you can manipulate with its built-in methods . For example, imagine you created a puzzle game that plays fun music while the user is playing. To make the game more exciting, when the player is about to lose, you can have the program switch to ominous music by loading in the appropriate track from the library.

You would have to create a Sound object and to do this, you must declare it; all you have to do is write the following line:

 var mySound = new Sound(); 

and you have yourself a Sound object of the Sound class.

Before you can play a sound from this object, you must attach the sound to the object. For this to work, you must have a sound imported into your library and have its linkage properties set up.

Import a sound into your library and access its Linkage Properties dialog box by accessing the context menu. The Linkage Properties dialog box should look like that in Figure 10.7. Give your sound a name and make sure you export it in the first frame. This name should be reflected in your Library window.

Figure 10.7. Adjusting the linkage properties for a sound


Once your file is prepared, you can either type in the following listing or play with demo file GDA_FIG10.2.fla. It takes an imported sound and loops it 9,999 times.

 // 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("beatTenOne"); // Play the sound starting at 0 pos // looping for 9,999 times. mySound.start(0, 9999); 

As I said before, when you create a new Sound object, you need to attach a sound to the object so it can play something. I have done this with the following line:

 mySound = new Sound(); 

Then I used the linkage name as the attachSound parameter to attach that sound to the mySound object like this:


The start method of the Sound class of object can actually be used with no parameters, as shown belowthis will cause the sound to be played once straight through. However, the way I used the start method in this demo is a differentthe first parameter indicates how far along (in milliseconds ) the Sound object should start playing the sound. This is good for sounds that have silent gaps in front of the actual contents. The second parameter actually indicates how many times you want the sound to loop.


Play the SWF; you'll find that it does what I just explained. It loops the sound 9,999 times because of this line:

 mySound.start(0, 9999); 

By the way, the sound you hear is my voice before I modified ityou can get really creative once you have a mic in front of your face.



Try modifying demo GDA_PROG10.2.flaexecute the start method with no parameters and hear what happens.

[ 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: