I want to have one sound object, and one short sound looping forever. I want that sound to fade in and I want a simple sound on, sound off button. I don’t want to use onEnterFrame either. Is there a way to do this without onEnterFrame?
This is the code I found but it uses onEnterFrame.
[AS]
//**********************************************
// create Sound obj and set volume at 0
//**********************************************
sndObj = new Sound();
sndObj.attachSound(“loop”);
sndObj.setVolume(0);
//**********************************************
// function to fade in the sound
//**********************************************
function fadeSnd(speed) {
this.onEnterFrame = function() {
vol += speed;
if (sndObj.getVolume() == 100) {
//trace(“sound at max!”);
delete this.onEnterFrame;
} else {
sndObj.setVolume(vol);
// show the fade
display = sndObj.getVolume();
}
};
}
//**********************************************
// button to activate snd and fadeSnd function
//**********************************************
sndBtnOn_mc.onPress = function() {
sndObj.start(0, 9999);
fadeSnd(1);
};
//*****************************
So how do I have it fade out when another button is pressed? Do I need to clear interval? Also, I tried adding this code, so that you can’t keep clicking the button when it’s already playing to have a bunch of loops playing at once but it didn’t work…
here i have make the start and stop function los from the onPress of the the buttons so now you can stop and start the sound with the
startSndObj() and stopSndObj() functions
sndObj = new Sound();
sndObj.attachSound("loop");
sndObj.setVolume(0);
sndObj.fadeIn = function(speed) {
var vol = this.getVolume()+speed;
this.setVolume(vol);
display = this.getVolume();
if (this.getVolume()>100) {
this.setVolume(100);
clearInterval(this.interval);
}
};
sndObj.fadeOut = function(speed) {
var vol = this.getVolume()-speed;
this.setVolume(vol);
display = this.getVolume();
if (this.getVolume()<0) {
this.setVolume(0);
this.stop();
this.playing = false;
clearInterval(this.interval);
}
};
startSndObj = function(){
if (!sndObj.playing) {
sndObj.start(0, 9999);
clearInterval(sndObj.interval);
sndObj.interval = setInterval(sndObj, "fadeIn", 1000/12, 1);
sndObj.playing = true;
}
};
stopSndObj = function(){
if (sndObj.playing) {
clearInterval(sndObj.interval);
sndObj.interval = setInterval(sndObj, "fadeOut", 1000/12, 1);
}
};
//------------------------------------------------------
startSndObj();//calls the start function
//asigns the onPress's to thair function
sndBtnOn_mc.onPress = startSndObj;
sndBtnOff_mc.onPress = stopSndObj ;