简体   繁体   中英

Restoring custom class object array from SharedObject

I have an array of Widgets (a class I created) called "widgetArray" that I save into a shared object.

savedGame1.data.widgetArray = widgetArray;

When I go to load this data out and use the widgets I run into problems. The array is stored fine, but each widget is saved as an object. So I can't simply do:

widetArray = savedGame1.data.widgetArray;

because then if I try to use the widgets:

widgetArray[0].someWidgetFunction();

it does not work because flash thinks they are objects. I have tried typecasting the widgets when I load them, but it only produces a null object.

widgetArray[i] = widgetArray[i] as Widget;//widgetArray[i] becomes null
tempWidget:Widget = widgetArray[i] as Widget;//null as well

This question is asked in one of the forums from a friend, since there were no responses, i thought this is the right place for it... How does anyone else deal with this?

Make two methods that can save and load a widget, the save method should take the data from the widget class and save it into the shared object the load class will take the data from the shared object and set the properties of the object.

Something like this :

 public function save():Object { return { "x":this.x, "y":this.y, "otherStuff":otherStuff }; } public function load(data:Object):void { this.x = data.x; this.y = data.y; this.otherStuff = data.otherStuff; } 

You can call the save method and store the results in an array and then store it in the shared object. You only need to save the data that is required to rebuild the widget class, not all the properties of the class.

Edit : Updated based on BlueRaja's comment.

As BlueRaja pointed out IExternalizable is meant to be used for this.

If you have a class like this :

 public class MyClass implements IExternalizable { private var one:int = 1; private var two:int = 2; public function MyClass() { } public function writeExternal(output:IDataOutput):void { output.writeInt(one); output.writeInt(two); } public function readExternal(input:IDataInput):void { one = input.readInt(); two = input.readInt(); } public function print():void { trace(one); trace(two); } } 

Then you can save it like this:

 registerClassAlias("MyClass", MyClass); var sharedObject:SharedObject = SharedObject.getLocal("so"); var myClass:MyClass = new MyClass(); sharedObject.data['storedObject'] = myClass; sharedObject.flush(); 

Then to load it :

 registerClassAlias("MyClass", MyClass); var sharedObject:SharedObject = SharedObject.getLocal("so"); var loadedClass:MyClass = sharedObject.data['storedObject'] as MyClass; loadedClass.print(); 

Hope that helps.

http://tush.wordpress.com/2007/07/08/actionscript-3-serializing-classes-using-registerclassalias/ ...这也是将自定义类对象存储在共享对象中的一种真正方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM