0

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 ();
}
user3082874
  • 61
  • 2
  • 11

1 Answers1

0

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.

moosefetcher
  • 1,841
  • 2
  • 23
  • 39
  • Hey, thanks for the extra heads help on that. I will have to give it a go when I get the chance. – user3082874 Feb 17 '14 at 17:46
  • Hmm, I tried to cut and paste the snippet above amending the "so" to mySO and "savedDate" and it threw back an error of an undefined property. So in order for that code to activate above (the commented out code) How do I get the load_btn to work and call the sharedObject? Excuse me for being slow. . . . – user3082874 Feb 18 '14 at 09:05
  • Perhaps I should have said - You will need to import flash.net.SharedObject; at the start of our Class and, in your variable definitions, you will need to declare 'so' as a SharedObject, ie private var so:SharedObject; Note that so = SharedObject.getLocal("sharedData") calls the static method 'getLocal' on the SharedObject Class; THAT returns a SharedObject object. Therefore you DON'T need to instantiate a SharedObject in the normal way (ie so = new SharedObject()). – moosefetcher Feb 18 '14 at 09:21
  • Also, to load your previous save on a button press: load_btn.addEventListener(MouseEvent.CLICK, loadPrevious); Then, in the loadPrevious handler, check if _cookiesActive = true and, if so, set your game variables to the corresponding ones in 'so', ie: bones_mc.x = so.data.my_x; (this assumes you have used the commented-out section of your code to save your variables to 'so'). – moosefetcher Feb 18 '14 at 09:39
  • I have added the working loading code the the script for you ref. bones_mc.x = mySO.data.my_x; bones_mc.y = mySO.data.my_y; Saves on the last X and Y of bones_mc Thanks – user3082874 Mar 07 '14 at 12:38