从共享对象恢复自定义类对象数组 [英] Restoring custom class object array from SharedObject

查看:183
本文介绍了从共享对象恢复自定义类对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的窗口小部件的阵列称为widgetArray我保存到一个共享对象(我创建了一个类)。

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?

推荐答案

使两个方法可以保存和加载一个小部件,保存方法应该从Widget类取数据,并保存到共享对象负载类会从共享对象中的数据和设置对象的属性。

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.

事情是这样的:

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;
}   

您可以调用保存方法和结果存储在一个数组,然后将其存储在共享对象研究。你只需要保存所需重建Widget类中的数据,而不是类的所有属性。

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.

编辑:更新基于BlueRaja的评论

Edit : Updated based on BlueRaja's comment.

正如BlueRaja指出IExternalizable旨在用于此

As BlueRaja pointed out IExternalizable is meant to be used for 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.

这篇关于从共享对象恢复自定义类对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆