00:00
00:00
Newgrounds Background Image Theme

jailander1 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Sound

35,498 Views | 101 Replies
New Topic Respond to this Topic

Response to AS: Sound 2005-10-01 18:05:59


At 10/1/05 06:04 PM, 2k_rammerizkool wrote: you forgot loadSound (:

it loads a sound dynamically from a URL. works kinda like this

loadSound("http://rammerzworld.com/sound.m
p3", true)

the "true" in there sets it to stream the sound, i think.

oops, sorry about double-post, but i forgot to mention that this only works with mp3 sounds \:


snyggys

Response to AS: Sound 2005-10-31 11:39:39


nice thank you really much

Response to AS: Sound 2005-10-31 12:10:06


Awesome Denver. Lol.

Thanks for another tut! :)


"Actually, the server timed out trying to remove all your posts..."

-TomFulp

Response to AS: Sound 2005-11-04 17:42:57


Is there any way to use Actionscripted sounds without having to "export in first frame?" I've got a game I'm making and want to use all Actionscripted sounds, but if I do that then I have to wait for the sounds to load before my preloader. Does anyone know any way to alleviate the problem?


BBS Signature

Response to AS: Sound 2005-11-04 18:39:27


Bump!


BBS Signature

Response to AS: Sound 2005-11-11 14:42:45


At 11/4/05 05:42 PM, Atomic_Sponge wrote: Is there any way to use Actionscripted sounds without having to "export in first frame?" I've got a game I'm making and want to use all Actionscripted sounds, but if I do that then I have to wait for the sounds to load before my preloader. Does anyone know any way to alleviate the problem?

If anyone is having a problem like this, I have finally found a solution. Just make a movie clip that is at least 2 frames long. In the first frame put a stop(); action and in the second frame put all of your sounds you want to export and set to Start or Event (make new keyframe for each of them). Then, take that movie clip and put it somewhere in the beginning of your movie, after your preloader and before you make any new sound objects. Now, any sound that is in the movie clip does not have to be set to "Export in First Frame" to be able to use. I can't believe this took me that long to figure out...


BBS Signature

Response to AS: Sound 2005-11-26 17:12:08


This is going to sound stupid:

What's an MC?

Response to AS: Sound 2005-12-28 22:04:58


At 11/26/05 05:12 PM, Gotzen-Dammerung wrote: This is going to sound stupid:

What's an MC?

Movieclip

Response to AS: Sound 2005-12-28 22:07:09


At 12/28/05 10:04 PM, Dislexsick wrote: Movieclip

Thank you for answering a month old question, I thought the world was going to end if it went unanswered. Or did you even look at when the last post was?

Response to AS: Sound 2006-02-15 00:26:00


How do you change the "instance name" of it when you're looping inside movie clips. I just renamed my movie clip "Sound01" and that doesn't work.

Response to AS: Sound 2006-02-15 00:45:03


I've figured out how to name the instance, but my flash still doesn't loop properly. I followed every instruction on the second part about looping in Movie Clips and as soon as my regular timeline is finished, the loop will just repeat over itself and so on until it gets hard to hear because of all the noise, what should I do?

Response to AS: Sound 2006-03-01 10:54:36


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

Response to AS: Sound 2006-03-01 11:13:21


Nice tutorial. I have been wondering how to do a lot of that with AS for a while.

Response to AS: Sound 2006-03-01 16:35:11


I did not test the code above so here is the working version with bug fixes:
(i also added an example fla ... see below)
[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 var fadeNum:Number;
public function SoundManager() {
currentMusic = 0;
// -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[num].setVolume(1);
fadeNum = 0;
fadeInterval = setInterval(_global.soundManager.fade, 10*duration);
}
public function fadeOut(duration:Number):Void {
// start fading out
clearInterval(_global.soundManager.fadeInt
erval);
/**************/
fadeNum = 1;
fadeInterval = setInterval(_global.soundManager.fade, 10*duration);
}
public function fade():Void {
var num = _global.soundManager.fadeNum;
// this gets handled by the soundManager it will fade a song
var pan:Number = _global.soundManager.music[_global.soundMa
nager.currentMusic].getVolume();
if (num) {
if (pan == 0) {
_global.soundManager.stopMusic();
clearInterval(_global.soundManager.fadeInt
erval);
} else {
_global.soundManager.music[_global.soundMa
nager.currentMusic].setVolume(pan-1);
}
} else {
if (pan == _global.MaxVolume) {
clearInterval(_global.soundManager.fadeInt
erval);
} else {
_global.soundManager.music[_global.soundMa
nager.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].setVolume(_global.MaxVolume);
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].setVolume(_global.MaxVolume);
sounds[num].start();
}
}
public function stopSound(num:Number):Void {
// stops a sound
sounds[num].stop();
}
}

[/code]

And to test the example go here:

http://www.dubhdroia..ch/test/Example.html

Here is the source to the example:

http://www.dubhdroia..est/SoundManager.zip

Response to AS: Sound 2006-03-01 23:13:27


Now it has also a pause feature for the music!

[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 var fadeNum:Number;
public var posBackup:Number;
// needed for pausing
public var musicPaused:Boolean;
public function SoundManager() {
currentMusic = 0;
// -1 is no selected song
musicStopped = true;
// music wont play at load
numberofSounds = 8;
// how many sounds are there?
numberofSongs = 2;
// how many songs?
musicPaused = false;
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[num].setVolume(1);
fadeNum = 0;
fadeInterval = setInterval(_global.soundManager.fade, 10*duration);
}
public function fadeOut(duration:Number):Void {
// start fading out
clearInterval(_global.soundManager.fadeInt
erval);
/**************/
fadeNum = 1;
fadeInterval = setInterval(_global.soundManager.fade, 10*duration);
}
public function fade():Void {
var num = _global.soundManager.fadeNum;
// this gets handled by the soundManager it will fade a song
var pan:Number = _global.soundManager.music[_global.soundMa
nager.currentMusic].getVolume();
if (num) {
if (pan == 0) {
_global.soundManager.stopMusic();
clearInterval(_global.soundManager.fadeInt
erval);
} else {
_global.soundManager.music[_global.soundMa
nager.currentMusic].setVolume(pan-1);
}
} else {
if (pan == _global.MaxVolume) {
clearInterval(_global.soundManager.fadeInt
erval);
} else {
_global.soundManager.music[_global.soundMa
nager.currentMusic].setVolume(pan+1);
}
}
}
public function pauseMusic():Void {
if (musicPaused) {
playMusic(currentMusic);
musicPaused = false;
} else {
posBackup = music[currentMusic].position;
stopMusic();
musicPaused = true;
}
}
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 || musicPaused) {
stopMusic();
//trace("start music: "+num);
music[num].setVolume(_global.MaxVolume);
if (posBackup != 0 && currentMusic == num) {
trace("unpausing");

music[num].start(Math.round(posBackup/1000
), 99999);
} else {
trace("normal playing");
music[num].start(0, 99999);
}
posBackup = 0;
musicPaused = false;
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].setVolume(_global.MaxVolume);
sounds[num].start();
}
}
public function stopSound(num:Number):Void {
// stops a sound
sounds[num].stop();
}
}

[/code]

And to test the example go here:

http://www.dubhdroia..ch/test/Example.html

Here is the source to the example:

http://www.dubhdroia..est/SoundManager.zip

Response to AS: Sound 2006-03-02 16:24:04


Okay i made two examples, one for games and one for movies.

Example one using the manager with buttons:

http://www.dubhdroia..t/Example1Games.html

Example two using it with frames:

http://www.dubhdroia../Example2Movies.html

The source with the as and fla files:

http://www.dubhdroia..est/SoundManager.zip

greets and enjoy

Response to AS: Sound 2006-03-02 16:25:34


At 3/2/06 04:24 PM, LeechmasterB wrote: Okay i made two examples, one for games and one for movies.

Thanks for these, I haven't had a reason to try them out yet, but your work is certainly appreciated.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Sound 2006-03-02 16:28:02


No problem your welcome! :)

Response to AS: Sound 2006-03-04 02:04:29


Sorry, had to delete the zip and example files because people dont want my shit here.. the code is still there anyways.

Response to AS: Sound 2006-03-29 15:36:03


how do i make a sound play when i click the mouse

var bc=1000; //bulletcount
_root.onMouseDown=function(){
bc++;
if(bc>1100){ bc=1000; } //Reset bulletcount
duplicateMovieClip("bullet", "b"+bc, bc); //Create dupe bullet
}

thats in the frame the sounds name is "uhhh"

please help

Response to AS: Sound 2006-06-23 23:10:11


At 3/29/06 03:36 PM, blingblingx1 wrote: how do i make a sound play when i click the mouse

i too need help with this :(

Response to AS: Sound 2006-06-29 00:59:55


wow thanks, i was looking for this tutorial

Response to AS: Sound 2006-07-15 06:15:44


How do I get 3 different sound buttons to work, so if you press for 1, it plays background music 1, press for 2, it plays background music 2 etc

Response to AS: Sound 2006-07-29 02:54:25


bah. i cant believe im having this problem. ok. This code works 100% right:

_root.foot = new Sound(this);
_root.foot.attachSound("FootStep");

and starting it at a different time. Now, what isn't working is, if i do this:

_root.laserSound = new Sound(this);
_root.laserSound.attachSound("laser");
_root.foot = new Sound(this);
_root.foot.attachSound("FootStep");

suddenly, the laser sound works fine, but the foot sound does not work at all. If i take out the two laser ligns again, its works 100% again. Anyone know what the problem could be?


BBS Signature

Response to AS: Sound 2006-07-29 03:01:15


ok. i figured something out. The footstep sound is only going when the laser sound is going. it cant be started without the laser sound being started, and when the laser sound ends, the foot sound ends. even though im saying _root.laser.stop(), it stops both. any ideas?


BBS Signature

Response to AS: Sound 2006-09-11 15:05:46


how do you make a button to change the sound,
srry if you don't understand that

how do you make a button that changes the current sound to a different sound, aka NG10283 to NG8294054

Response to AS: Sound 2006-09-11 15:55:06


sombody please help
(hte person who gives me the answer goes on my credits)

Response to AS: Sound 2006-10-21 18:23:32


Err, i Still don't get it... It says that the music i import has trouble being read or something... So i can't really post anything on newgrounds 'cause they're always blammed because of no sounds... w\e help plz.

Response to AS: Sound 2006-10-21 18:24:39


At 10/21/06 06:23 PM, Stobble wrote: Err, i Still don't get it... It says that the music i import has trouble being read or something... So i can't really post anything on newgrounds 'cause they're always blammed because of no sounds... w\e help plz.

that has nothing to do with this tutorial, its because the format of the song your importing is not reconized by flash. what format is it in? it should be mp3 or wav ro work properly.


BBS Signature

Response to AS: Sound 2007-01-18 22:06:10


Thank you, this helped me out alot.
But I have it playing on two frames, and when you go back to the first one, it starts it over, while the first one is still playing