0

I have a drag and drop project where the user can drag copies of a MovieClip, and I would like to know how to save and load the position of the draggable copies using the SharedObject class. I currently use SharedObject with a save and a load button. But it only saves the position of the last dragged MovieClip copy.

How do I save the position of the MovieClip copies on stage, so that when I click the load button it loads the position of all copies as they were positioned when I clicked the save button?

Code:

latestClone.addEventListener(MouseEvent.MOUSE_DOWN, onCloneClick);
function onCloneClick(e:MouseEvent):void
{
var target:MovieClip = e.currentTarget as MovieClip;
latestClone = target;



}

}   

save.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

function fl_MouseClickHandler(event:MouseEvent):void


var mySo:SharedObject = SharedObject.getLocal("SaveData");

mySo.data.my_x = latestClone.x;
mySo.data.my_y = latestClone.y;
mySo.flush();


}


loader.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

function fl_MouseClickHandler_2(event:MouseEvent):void
{
var mySo:SharedObject = SharedObject.getLocal("SaveData");

latestClone.x = mySo.data.my_x;
latestClone.y = mySo.data.my_y;


}   
New2AS3
  • 36
  • 6
  • How is anyone supposed to read that? Edit your question and put the code there. –  Mar 04 '16 at 11:17
  • There a lot of answers on this one: http://stackoverflow.com/questions/30125221/using-file-to-save-scene-object-locations-to-rebuild-later-in-as3/30131304#30131304 – BadFeelingAboutThis Mar 04 '16 at 16:54

1 Answers1

0

Using SharedObject does mean that your app is not in charge of the data persistence, the OS or the user can remove that data or even not allow that data at all at any time.

So first problem, your code is using that data like it will always exist. Wrong way, first thing to do is check if that data is available, ex:

var globaldata:Object;
var data:Object = SharedObject.getLocal("SaveData").data;
if(data["mydata"] != undefined)
{
    //there's some data here
    globaldata = data["mydata"];
}
else
{
    //there's no data
    globaldata = {};
}

Next keep a global object and put your values in it:

globaldata["mc1"] = {x:mc1.x, y:mc1.y};
globaldata["mc2"] = {x:mc2.x, y:mc2.y};
//etc .....
data["mydata"] = globaldata;
//no need to call flush

Now you are ready to use that data if it's still there:

mc1.x = data["mydata"]["mc1"].x;
//etc ....
BotMaster
  • 2,233
  • 1
  • 13
  • 16