What i posted is an as 2.0 class which you can use with flash mx or later. You can reuse it for any games and also add more functions and additional functionality. It is very simple and easy to understand if you look at it carefully.
[code]
class SoundManager {
public var sounds:Array;
// An array that holds all game sounds
public var music:Array;
// An array that holds the game music
public var musicStopped:Boolean;
// is the music stoped?
public var currentMusic:Number;
// song playing right now
public var numberofSounds:Number;
// total number of sounds
public var numberofSongs:Number;
// total number of sounds
public var fadeInterval;
// a var for the fade interval
public function SoundManager() {
currentMusic = -1;
// -1 is no selected song
musicStopped = true;
// music wont play at load
numberofSounds = 8;
// how many sounds are there?
numberofSongs = 2;
// how many songs?
sounds = new Array(numberofSounds);
for (var i = 0; i<sounds.length; i++) {
sounds[i] = new Sound();
sounds[i].attachSound("demoSound"+i+".mp3"
);
// Attach all sounds to sound objects and put them into the sounds array
// The sounds need to have a linkage identifier called demoSound"#NUMBER".mp3
}
/*********************************/
music = new Array(numberofSongs);
for (var i = 0; i<music.length; i++) {
music[i] = new Sound();
music[i].attachSound("music"+i+".mp3");
// Attach all songs to sound objects and put them into the sounds array
// The songs need to have a linkage identifier called music"#NUMBER".mp3
}
_global.soundManager = this;
// a global reference to this class
}
public function killAll():Void {
for (var i = 0; i<sounds.length; i++) {
stopSound(i);
}
/*********************************/
stopMusic();
// kills all sounds and turns the music off
}
public function fadeIn(duration:Number, num:Number):Void {
// start fading in
clearInterval(_global.soundManager.fadeInt
erval);
/**************/
playMusic(num);
music[currentMusic].setVolume(0);
fadeInterval = setInterval(_global.soundManager.fade(0), 10*duration);
}
public function fadeOut(duration:Number):Void {
// start fading out
clearInterval(_global.soundManager.fadeInt
erval);
/**************/
fadeInterval = setInterval(_global.soundManager.fade(1), 10*duration);
}
public function fade(num:Number):Void {
// this gets handled by the soundManager it will fade a song
var pan:Number = music[currentMusic].getVolume();
if (num) {
if (pan == 0) {
stopMusic();
clearInterval(_global.soundManager.fadeInt
erval);
} else {
music[currentMusic].setVolume(pan-1);
}
} else {
if (pan == 100) {
clearInterval(_global.soundManager.fadeInt
erval);
} else {
music[currentMusic].setVolume(pan+1);
}
}
}
public function playMusic(num:Number):Void {
// if _global.soundON is set true
//(a global variable storing weather sound is on or of)
// play the song passed with the number
if (_global.soundON) {
if (currentMusic != num || musicStopped) {
stopMusic();
//trace("start music: "+num);
music[num].start(0, 99999);
currentMusic = num;
musicStopped = false;
}
}
}
public function stopMusic():Void {
// stops the song currently playing
music[currentMusic].stop();
musicStopped = true;
}
public function playSound(num:Number):Void {
// plays a sound
if (_global.soundON) {
sounds[num].start();
}
}
public function stopSound(num:Number):Void {
// stops a sound
sounds[num].stop();
}
}
[/code]
I added the functions to fade sound....
The code above needs to be in a file called SoundManager.as
In the game you can use it like this after you set the number of songs and sounds in teh as file correctly and have the correct named files in your fla with the linkage identifier set:
var soundman = new SoundManager();
// creates a new instance of the soundmanager, you need only one, Its a singleton class.
If you for example have 8 sounds and 2 songs:
soundman.playMusic(1);
Plays song number two 9999 times or until stoped or another song is launched. And soundman.stopMusic(); will stop it again.
soundman.playSound(3);
Plays sound number two once but it can also be stoped early with:
soundman.stopSound(3);
To fade a sound in over a time of 20 seconds you call this:
soundman.fadeIn(20, 1);
or to fade it out again:
soundman.fadeOut(20);
Nobody is forcing you to use the comfort of oop and recycling code without ever having to worry about it again. ;) I did not test this but it should work and if it does not work then the problem is that the class properties cannot be accessed from within the fade(num:Number) function because its called by an interval. So you need to fully reference every function call and variable to the soundmanager in that function if it should not work and everything else is set up right.
greets