Search code examples
flashactionscriptactionscript-2flash-cs3

Shared Objects don't work in my code


However, they work in other examples, even locally, when I download the source.

Code:

//Button
on(release)
{
    onr_save = SharedObject.getLocal("onr");
    onr_save.saved = "true";
    //onr_save.flush();
    trace("kk: "+onr_save.saved);
}

.

//Frame
onr_save = SharedObject.getLocal("onr");
load_game._visible=false;
if(_root.onr_save.data.saved=="true")
{
    load_game._visible=true;
}

It always says "kk: true" when I press the button, but when I recompile, the button is invisible and the trace says "undefined". What am I doing wrong?


Solution

  • Local shared objects have a data property (it's an object) that you have to use to hold the data you want stored.

    You're using that in the code that GETS the LSO, but not in the code that SETS the LSO.

    So you first example will just change to this:

    //Button
    on(release)
    {
        onr_save = SharedObject.getLocal("onr");
        onr_save.data.saved = "true";
        //onr_save.flush();
        trace("kk: "+onr_save.saved);
    }
    

    On an unrelated note, this method of writing code directly on buttons is only supported in ActionScript 2.0 and earlier - which has been deprecated for a number of years now. You would do well to start learning ActionScript 3.