问题
I have searched for an answer to this question for quite a while but can't seem to find a definite one. Basically what I am trying to do is take an Array and store it within a shared object, and then read what is stored in the object and put it back into an array list when the script is started (hope this makes sense).
At the moment I am led to believe that shared objects can only store multiple variables, so I am not sure how I would go about adding an array to one.
The array itself is declared as shown here:
var lvl1highScores:Array = new Array();
And it can be added to by the user every time they reach a high score in each level as so:
lvl1highScores.push({score:int(vinylCollected) , player:String(highScoreInput.text)});
lvl1highScores.sortOn("score", Array.DESCENDING | Array.NUMERIC);
I hope this is enough information for someone to help out. If I am unable to store the whole array in a shared object, is there any other way of storing this information?
Cheers
回答1:
You should be able to do this,
To write to a shared object
//returns the mySharedObject if it exists, if not creates a new one
var so:SharedObject = SharedObject.getLocal("mySharedObject");
//take your array and put it on the so
so.data.storedArray = myArray;
//save the data
so.flush();
To read it back elsewhere
//get the mySharedObject back
var so:SharedObject = SharedObject.getLocal("mySharedObject");
//get your array back
myArray = so.data.storedArray;
回答2:
I found something helpful that I'm going to add into the above code answer:
To read it back elsewhere
//get the mySharedObject back
var so:SharedObject = SharedObject.getLocal("mySharedObject","/");
//get your array back
myArray = so.data.storedArray;
To avoid inadvertently restricting access to a shared object, use the localpath parameter. - which is the "/"
来源:https://stackoverflow.com/questions/14010707/as3-store-array-in-shared-object