Actionscript Code:
var saveState:SharedObject = SharedObject.getLocal("cookie");
music = new Sound();
music.loadSound ("music.mp3", true);
if(saveState.data.pressed != undefined){
pressed = saveState.data.pressed;
checkMute();
} else {
pressed = false;
checkMute();
}
btn.onPress = function(){
pressed = !pressed;
checkMute();
}
function checkMute(){
if(pressed == true){
btn.gotoAndStop(2);
music.setVolume(0);
} else {
btn.gotoAndStop(1);
music.setVolume(100);
}
}
Actionscript Code:
var saveState:SharedObject = SharedObject.getLocal("cookie");
// declare sound first, so that you may change its volume depending on the saved value,
// otherwise if you declare it after we mute/unmute it depending on the saved variable's value
// the sound will not mute, because the sound object has not been declared yet, so what to mute?
music = new Sound();
music.loadSound ("music.mp3", true);
// check if saved value is declared/created
if(saveState.data.pressed != undefined){
// if yes, then set pressed's value as the saved value
pressed = saveState.data.pressed;
// execute this function, to execute the same set of code
// by simply calling that function, saves a lot of time and is easy to edit
checkMute();
} else {
// if not, then set default value as FALSE
pressed = false;
checkMute();
}
btn.onPress = function(){
// this changes pressed's value to the opposite, if it's TRUE,
// then it will turn to FALSE, and vice versa
pressed = !pressed;
checkMute();
}
// the actual function
function checkMute(){
// check if pressed's value is TRUE
if(pressed == true){
// if it is, mute sound
btn.gotoAndStop(2);
music.setVolume(0);
} else {
// otherwise, unmute sound
btn.gotoAndStop(1);
music.setVolume(100);
}
}