Adding Music and Sound FX With Flex and Actionscript

Written by:  • Edited by: Linda Richter
Updated Nov 6, 2009
• Related Guides: Music

Now, we're really starting to see the whole game together as we take a look at how to add music and a few sound effects.

In part 7 we added the ability to play animations, which then gave us some nice explosions. At this point we almost have something resembling a game, except for one: sound. In this article we will add some sound and music to the game.

Fortunately for us Flex makes embedding, playing and transforming sounds very easy. The first logical place to start is by adding some sounds to our ResourceManager.

Pump Up the Volume

package

{

import flash.display.*;

import mx.core.*;

public final class ResourceManager

{

[Embed(source="../media/brownplane.png")]

public static var BrownPlane:Class;

public static var BrownPlaneGraphics:GraphicsResource = new GraphicsResource(new BrownPlane(), 3, 20);

[Embed(source="../media/smallgreenplane.png")]

public static var SmallGreenPlane:Class;

public static var SmallGreenPlaneGraphics:GraphicsResource = new GraphicsResource(new SmallGreenPlane(), 3, 20);

[Embed(source="../media/smallblueplane.png")]

public static var SmallBluePlane:Class;

public static var SmallBluePlaneGraphics:GraphicsResource = new GraphicsResource(new SmallBluePlane(), 3, 20);

[Embed(source="../media/smallwhiteplane.png")]

public static var SmallWhitePlane:Class;

public static var SmallWhitePlaneGraphics:GraphicsResource = new GraphicsResource(new SmallWhitePlane(), 3, 20);

[Embed(source="../media/bigexplosion.png")]

public static var BigExplosion:Class;

public static var BigExplosionGraphics:GraphicsResource = new GraphicsResource(new BigExplosion(), 7, 20);

[Embed(source="../media/smallisland.png")]

public static var SmallIsland:Class;

public static var SmallIslandGraphics:GraphicsResource = new GraphicsResource(new SmallIsland());

[Embed(source="../media/bigisland.png")]

public static var BigIsland:Class;

public static var BigIslandGraphics:GraphicsResource = new GraphicsResource(new BigIsland());

[Embed(source="../media/volcanoisland.png")]

public static var VolcanoIsland:Class;

public static var VolcanoIslandGraphics:GraphicsResource = new GraphicsResource(new VolcanoIsland());

[Embed(source="../media/twobullets.png")]

public static var TwoBullets:Class;

public static var TwoBulletsGraphics:GraphicsResource = new GraphicsResource(new TwoBullets());

[Embed(source="../media/cloud.png")]

public static var Cloud:Class;

public static var CloudGraphics:GraphicsResource = new GraphicsResource(new Cloud());

[Embed(source="../media/gun1.mp3")]

public static var Gun1Sound:Class;

public static var Gun1FX:SoundAsset = new Gun1Sound() as SoundAsset;

[Embed(source="../media/explosion.mp3")]

public static var ExplosionSound:Class;

public static var ExplosionFX:SoundAsset = new ExplosionSound() as SoundAsset;

[Embed(source="../media/track1.mp3")]

public static var Track1Sound:Class;

public static var Track1FX:SoundAsset = new Track1Sound() as SoundAsset;

}

}

Here we embed the sound files just like we do for our graphics. This packages the sounds in the final SWF file giving us a convenient way to distribute and access them.

Now that we have made the sounds available we need to play them. Using the sound effects here we have an explosion that will be played when the player or an enemy dies, a sound for when the player fires weapons, and some background music to play while in the game.

Lets take a look at the Enemy class to see how to play a sound effect.

Showing page 1 of 2

Comments

Showing all 3 comments
 
Rolpege Dec 11, 2009 2:34 PM
RE: Adding Music and Sound FX With Flex and Actionscript
You can also add an event listener to the sound for when it's finishied... wait, it's a custom class that doesn't extends nothing that can dispatch events... so I cannot insert an event listener to it, right?

Well, I think that you can save in a variable the duration of the song, and in the loop, when it reaches the duration, you play it again.
Matthew Casperson Oct 26, 2009 2:07 AM
RE: Adding Music and Sound FX With Flex and Actionscript
Thats right. There is no way (that I know of) to loop indefinitely, but looping int.MAX_VALUE times does essentially the same thing.
Aryadi Oct 26, 2009 1:59 AM
Sound
so, the ResourceManager.ExplosionFX.play(); only plays a music/sound effects once and then automatically deletes it, while backgroundMusic = ResourceManager.Track1FX.play(0, int.MAX_VALUE); plays a music/sound effects in a loop until we call stop()

is my understanding correct?
 
blog comments powered by Disqus
Email to a friend