Hello again! I have this Mute/Unmute button for a music.
I have the following code , for when you click the button a first time, do an action (mute music), and then when you hit the button a 2nd time, undo that action (unmute music).
Actionscript Code:
var pressed=false; //tell flash that the button has not been pressed yet
btn.onPress=function(){//pressing the button for the first time if(pressed==false){//if the button has not been pressed yet, pressed=true; //tell flash the button has been pressed 1st time
this.gotoAndStop(2); //button pressed frame music.setVolume(0); //mute music
} else{//if the button has been pressed the 1st time,
music.setVolume(100); //unmute music pressed=false; //button has been pressed 2nd time this.gotoAndStop(1); //button released frame
}
That works 100%. Now i want to store these variables in a Shared Object, so when you load the flash the first time, and press the button to mute the music, when you refresh the page, the button remains muted and pressed.
I think the thing goes like this. I create the Shared object variable
Actionscript Code:
var saveState:SharedObject.getLocal("cookie");
Then in the button code, when muting and unmuting the music and setting pressed=true or pressed=false
Actionscript Code:
btn.onPress=function(){//pressing the button for the first time if(pressed==false){//if the button has not been pressed yet,
pressed=true; //tell flash the button has been pressed 1st time
saveState.data.pressed=pressed; //save
this.gotoAndStop(2); //button pressed frame music.setVolume(0); //mute music
} else{//if the button has been pressed the 1st time,
music.setVolume(100); //unmute music pressed=false; //button has been pressed 2nd time
saveState.data.pressed=pressed; //save
this.gotoAndStop(1); //button released frame
}
and then in the firstframe make an onLoad function to Load the Shared Object automatically
What you're doing, is that you're just assigning the value of pressed, again to your SharedObject's variable, what you instead want, is to assign the SharedObject's variable's value as pressed's value, and also, your declaration of SharedObject is wrong, you need to store it in a variable:
Actionscript Code:
var saveState:SharedObject = SharedObject.getLocal("cookie"); pressed = saveState.data.pressed;
Also, onLoad function is not needed, as the codes on your Frames will execute in a linear order from top to bottom once you enter that Frame, onLoad however executes once a MovieClip is loaded, and since _root is also a movieclip, it means that onLoad will only work on Frame 1
17 Years old boy, who loves the Computer Technology
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
Ok, i achieved to write and load the shared object. The only problem now is the default value is "reversed". I cleared the shared object to reset it to factory variable values, and instead of showing the button unpressed with the music playing, it shows the button pressed and the music muted. Only rests that, if I achieved to reverse de code, it will work.
Actionscript Code:
var saveState:SharedObject= SharedObject.getLocal("cookie"); pressed=saveState.data.pressed;
//button for reset to factory values for testing purposes cacheEraser.onPress=function(){
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 = newSound(); 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); } }
Hope this helps
17 Years old boy, who loves the Computer Technology
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
I understood your code 100%, and it's well written. But isn't working. The button works, it change states, but is not saving to the saveState shared object. I copied and pasted it and it looks now like this:
Actionscript Code:
var saveState:SharedObject = SharedObject.getLocal("cookie");
// declare sound first, so that you may change its volume depending on the saved value
music = newSound(); 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); } }
stop();
I think we are almost reaching the success point...thanks for helping again Nig 13.
Another Question that is off topic, i wish to help others with what i know already about flash, how I read new as2 threads in this forum, so I can help? Thx
Now it will save the pressed variable's value after each button press, I really feel stupid for overlooking that
And are you kidding me, you're actually asking to help others?? You don't even need to ask, just start helping others at your own will. It's great that you want to do that, as you will be helping others, while at the same time build up your experience in Flash
17 Years old boy, who loves the Computer Technology
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS