[RESOLVED] Shared Object
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] Shared Object

  1. #1
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    936

    resolved [RESOLVED] Shared Object

    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
    Actionscript Code:
    onLoad=function(){
      saveState.data.pressed=pressed;
    }

    But not working. Any help? Thanks

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,808
    Actionscript Code:
    onLoad=function(){
        saveState.data.pressed=pressed;
    }

    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
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    936
    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(){
       
        saveState.clear();
    }

    //shared object loader with shared object reference
    if (saveState.data.pressed==false) {
        pressed=true;
         btn.gotoAndStop(2);
         music.setVolume(0);
        trace("saveState pressed");
    }
    else {
        pressed=false;
        music.setVolume(100);
        btn.gotoAndStop(1);
        trace("saveState not pressed");
    }

    //shared object loader with root variable reference
    if (pressed==false) {
        pressed=true;
         btn.gotoAndStop(2);
         music.setVolume(0);
        trace("root pressed");
    }
    else {
        pressed=false;
        music.setVolume(100);
        btn.gotoAndStop(1);
        trace("root not pressed");
    }


    //Mute/Unmute Button
    btn.onPress=function(){

        if(pressed==false){
       
           
            saveState.data.pressed=true; //write variable value in shared object
           
            this.gotoAndStop(2);
            music.setVolume(0);
            trace("saved");
           
        }
        else {
           
            music.setVolume(100);
           
            saveState.data.pressed=false; //write variable value in shared object
           
            this.gotoAndStop(1);
            trace("saved");
        }
    }




    Here is the .flahttp://sofistica2.zzl.org/Mute%20Unmute.fla
    Last edited by angelhdz; 04-13-2012 at 05:30 AM.

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,808
    Then just change the order of the value saved to your SharedObject

    Btw, your code can be simplified:

    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);
        }
    }

    Explanation:

    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);
        }
    }

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    936
    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 = 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);
        }
    }

    stop();

    I think we are almost reaching the success point...thanks for helping again Nig 13.

    Here is the FLA UPDATED http://sofistica2.zzl.org/Mute%20Unmute.fla

    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
    Last edited by angelhdz; 04-13-2012 at 06:25 PM.

  6. #6
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,808
    Oh, hahahhahahahhhahahahahhahahah xD
    I forgot to save the new value to the SharedObject *facepalm*

    Add this line in the btn's onPress event, after the switching of the pressed's value:

    Code:
    saveState.data.pressed = pressed;
    or to make it simpler, just replace your btn.onPress function to this:

    Actionscript Code:
    btn.onPress = function(){
        pressed = !pressed;
        saveState.data.pressed = pressed;
        checkMute();
    }

    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
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  7. #7
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    936
    Thanks again m8. It's working like a charm. It's my first time with Shared Objects. Now I understand 'em. Take care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com