PDA

View Full Version : Sound fading in function with prototype and setInterval



marc0047
August 4th, 2005, 03:02 PM
I'm trying to create a function that will help me fade in sounds with the help of setInterval. I'm almost there! Here is the code so far:

// Begin

melodyhalf1 = new Sound("melodyhalf1_mc");
melodyhalf1.attachSound("melodyhalf1");
melodyhalf1.start(0, 99);
melodyhafl1.vol = 0;
melodyhalf1.setVolume(melodyhalf1.vol);

Sound.prototype.fadeIn = function() {
trace("I'm in fadeIn");
trace(this);
this.vol += 3;
this.setVolume(this.vol);
if (this.vol>=100) {
clearInterval(this.fade);
trace("cleared");
}
}

melodyhalf1.fade = setInterval(melodyhalf1.fadeIn, 500);

// End

I placed a comment in the Sound.prototype.fadeIn to test if it was being called, as well as to see if the melodyhalf1 variable (object?) was being passed.

The results are that the fadeIn function *is* being called, but the trace(this) comes back with 'undefined'.

What am I doing wrong?

bandinopla
August 4th, 2005, 03:31 PM
I don't know how to explain it, but, better do this:

// Begin

melodyhalf1 = new Sound("melodyhalf1_mc");
melodyhalf1.attachSound("melodyhalf1");
melodyhalf1.start(0, 99);
melodyhafl1.vol = 0;
melodyhalf1.setVolume(melodyhalf1.vol);

fadeIn = function(tsound) {
trace("I'm in fadeIn");
trace(tsound);
tsound.vol += 3;
tsound.setVolume(tsound.vol);
if (tsound.vol>=100) {
clearInterval(tsound.fade);
trace("cleared");
}
}

melodyhalf1.fade = setInterval(fadeIn(melodyhalf1), 500);


///////

setInterval( function,interval[,arg1, arg2, ...,argn] )

marc0047
August 4th, 2005, 04:12 PM
Thank you! I for some reason thought that arguments could not be passed through the setInterval. Thanks for pointing that out for me. I got the code to work, and if anyone is interested in the functions:

_global.soundFadeIn = function (soundObj) {
soundObj.vol += 10;
trace(soundObj.vol);
soundObj.setVolume(soundObj.vol);
if (soundObj.vol>=100) {
clearInterval(soundObj.fade);
trace("cleared");
}
}

_global.soundFadeOut = function (soundObj) {
soundObj.vol -= 10;
trace(soundObj.vol);
soundObj.setVolume(soundObj.vol);
if (soundObj.vol<=0) {
clearInterval(soundObj.fade);
trace("cleared");
}
}

// Once you've attached the sound to an object, call the function this way:
sound_object.fade = setInterval(soundFadeIn, 500, sound_object);

// the same thing with fading out
sound_object.fade = setInterval(soundFadeOut, 500, sound_object);

Be sure to clear the intervals if a sound object is in the process of fading up or down.

bandinopla
August 4th, 2005, 04:15 PM
:thumb:

brachford
March 21st, 2007, 01:51 AM
Thank you! I for some reason thought that arguments could not be passed through the setInterval. Thanks for pointing that out for me. I got the code to work, and if anyone is interested in the functions:

_global.soundFadeIn = function (soundObj) {
soundObj.vol += 10;
trace(soundObj.vol);
soundObj.setVolume(soundObj.vol);
if (soundObj.vol>=100) {
clearInterval(soundObj.fade);
trace("cleared");
}
}

_global.soundFadeOut = function (soundObj) {
soundObj.vol -= 10;
trace(soundObj.vol);
soundObj.setVolume(soundObj.vol);
if (soundObj.vol<=0) {
clearInterval(soundObj.fade);
trace("cleared");
}
}

// Once you've attached the sound to an object, call the function this way:
sound_object.fade = setInterval(soundFadeIn, 500, sound_object);

// the same thing with fading out
sound_object.fade = setInterval(soundFadeOut, 500, sound_object);

Be sure to clear the intervals if a sound object is in the process of fading up or down.


To anyone that is willing to lend a hand ... I would truly appreciate it. I have three goals to attain - outlined below.

GOAL #1
... is to have two sound files available that will simply play in the background of my site - Sound #1 will initially be loaded and begin to play. I will include a button for each sound ... if the user selects the button for Sound #2 ... that would (ideally) trigger Sound #1 to fade 'out' as Sound #2 fades 'in' and vice versa. If Sound #2 is playing and the user selects the button for Sound #1 ... that would trigger Sound #2 to fade 'out' as Sound #1 fades 'in'.

To whomever is lending their hand ... please set the Sound #1 identifier to 'Birds' and the Sound #2 identifier to 'Tranquil'. On a side note, as long as the code works, the trace commands can be removed.


GOAL #2
I mentioned in Goal #1, that Sound #1 will be loaded and begin to play. Ideally the sound will be loaded and fade 'in'. Code supplied to assist me with this would be sweet. As noted above (in Red) ... the identifier for Sound #1 will be set to 'Birds'.


GOAL #3
In addition to having a button for each of the two sound files ... I plan to also implement a "mute" button that will trigger whichever sound that is currently playing to fade out. Help with attaining this would be awesome too.

Thank you in advance for the help.
Brett