Question

I'm bumbling my way through AS3 I have come back to the idea of wanting to be able to load my last save from a sharedObject. Any help would be appreciated as i'm still a novice to AS3. I'm also wanting to attach it to a load btn.

// SAVE FUNCTIONS ---------------------------------------
//---------------------------------------------------
//---------------------------------------------------

var mySO:SharedObject = SharedObject.getLocal("iDesign");

bones_mc.x = mySO.data.my_x;
bones_mc.y = mySO.data.my_y;

if (!mySO.data.my_y) {
bones_mc.x = 424;
bones_mc.y = 119;
}

//---- THIS IS THE SAVER BTN
save_btn.addEventListener (MouseEvent.CLICK, clickersave);

function clickersave (e:MouseEvent):void {
mySO.data.my_x = bones_mc.x;
mySO.data.my_y = bones_mc.y;
mySO.data.mybut_x = btrfly_mc.x;
mySO.data.mybut_y = btrfly_mc.y;
mySO.data.mytig_x = tiger_mc.x;
mySO.data.mytig_y = tiger_mc.y; 
mySO.data.mybow_x = pink_bow_mc.x;
mySO.data.mybow_y = pink_bow_mc.y;      
mySO.flush ();
}
//----
bones_mc.buttonMode=true;

btrfly_mc.x = mySO.data.mybut_x;
btrfly_mc.y = mySO.data.mybut_y;

if (!mySO.data.mybut_y) {
btrfly_mc.x = 112;
btrfly_mc.y = 295;
}

btrfly_mc.buttonMode=true;

tiger_mc.x = mySO.data.mytig_x;
tiger_mc.y = mySO.data.mytig_y;

if (!mySO.data.mytig_y) {
tiger_mc.x = 804;
tiger_mc.y = 411;
}

tiger_mc.buttonMode=true;

pink_bow_mc.x = mySO.data.mybow_x;
pink_bow_mc.y = mySO.data.mybow_y;

if (!mySO.data.mybow_y) {
pink_bow_mc.x = 923;
pink_bow_mc.y = 579;
}

load_btn.addEventListener (MouseEvent.CLICK, loadlast);

function loadlast (e:MouseEvent):void {
bones_mc.x = mySO.data.my_x; 
bones_mc.y = mySO.data.my_y
//mySO.data.mybut_x = btrfly_mc.x;
//mySO.data.mybut_y = btrfly_mc.y;
//mySO.data.mytig_x = tiger_mc.x;
//mySO.data.mytig_y = tiger_mc.y;   
//mySO.data.mybow_x = pink_bow_mc.x;
//mySO.data.mybow_y = pink_bow_mc.y;        
mySO.flush ();
}
Was it helpful?

Solution

When I want to check if there is a Flash cookie for the game someone is playing I declare a _cookiesActive:Boolean and use the following code:

try {
    so = SharedObject.getLocal("savedData");
    _cookiesActive = true;
} catch (error) {
    _cookiesActive = false;
};

Then, if _cookiesActive == true, you can access so.data and any variables you might have set the last time the game was played, ie:

bones_mc.x = so.data.my_x;
bones_mc.y = so.data.my_y;

..and so on.
By the way, the commented out code in your sample above will SAVE variables to the SharedObject, not load them. So you already have the code to save them there.

Loading them on a button click is, as you suggest, just a matter of having the code that passes variables from your SharedObject to your game variables in the handler function for the CLICK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top