/**
Example of saving classes to shared objects
*/
// Person Class / Prototype Object
function Person(p_sex){
// not saved to SharedObject
this.m_sex = p_sex;
}
Person.prototype.setName = function(p_name){
this.m_name = p_name;
}
Person.prototype.setAge = function(p_age){
this.m_age = p_age;
}
Person.prototype.getName = function(){
trace("Name: "+this.m_name);
}
Person.prototype.getAge = function(){
trace("Age: "+this.m_age);
}
Person.prototype.getSex = function(){
trace("Sex: "+this.m_sex);
}
// Deserilizes custom class in ShareObject
Object.registerClass("Person",Person);
// Initializes SharedObject
var SavedData_so = SharedObject.getLocal("PersonData");
// Creates and saves SharedObject if it doesnt already
exist
if(SavedData_so.data.Person == null){
trace("SharedObject Created");
// Parameters in constructor are NOT saved
SavedData_so.data.Person = new Person("male");
SavedData_so.data.Person.setName("Erik");
SavedData_so.data.Person.setAge(25);
SavedData_so.flush();
}else{
trace("SharedObject Retrieved");
}
// Call SharedObjects custom class methods
SavedData_so.data.Person.getName();
SavedData_so.data.Person.getAge();
SavedData_so.data.Person.getSex();
// Output
// First Run: Creates / Saves
SharedObject Created
Name: Erik
Age: 25
Sex: male
// Second Run Retrieves SharedObject
// as you can see, parameters specified in the constructor are not saved
correctly.
SharedObject Retrieved
Name: Erik
Age: 25
Sex: undefined // Insert joke here |