SharedObject支持哪些数据类型?

时间:2022-06-28 16:30:05

I know it is a simple matter to store Strings and Numbers with a SharedObject, and I am also familiar with this sort of thing:

我知道使用SharedObject存储字符串和数字是一件简单的事情,我也熟悉这种事情:

var sharedObject:SharedObject = SharedObject.getLocal("userData");
var obj:Object = new Object();
obj.prop = "value";
sharedObject.data.userobj= obj;
sharedObject.flush();

However, I am attempting to store an object of the class GameStage, a class I have defined to hold data about stages in my game. This type of thing doesn't seem to be working:

但是,我正在尝试存储GameStage类的对象,这是我定义的类,用于保存游戏中各阶段的数据。这种类型的东西似乎不起作用:

var sharedObject:SharedObject = SharedObject.getLocal("userData");
var stageOne:GameStage = new GameStage();
stageOne.highScore = 99999;
sharedObject.data.stageOne = stageOne;
sharedObject.flush();

This code doesn't throw an error, but when I try to retrieve the stage data later, like so:

此代码不会引发错误,但是当我尝试稍后检索阶段数据时,如下所示:

stageOne = sharedObject.data.stageOne;

I get this error:

我收到此错误:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@3d220629 to GameStage.

I guess my question is: exactly what sort of data types can be stored in a SharedObject? Everywhere I've looked online has answered that question with "anything that can be used in Flash", which isn't very descriptive – obviously my GameStage class works in Flash too. Is there something about retrieving data from the SharedObject that I'm not aware of?

我想我的问题是:究竟什么类型的数据类型可以存储在SharedObject中?我在网上看到的所有地方都回答了“可以在Flash中使用的任何东西”的问题,这不是很具描述性 - 显然我的GameStage类也适用于Flash。有什么关于从SharedObject中检索我不知道的数据吗?

My prediction is that I will not be able to store my stage data this way. If that is the case, could anyone suggest an alternative method to saving the data?

我的预测是我无法以这种方式存储我的舞台数据。如果是这种情况,有人可以建议另一种方法来保存数据吗?

2 个解决方案

#1


You can store any object in a SharedObject, but you need to register the class first:

您可以将任何对象存储在SharedObject中,但您需要首先注册该类:

You can store typed ActionScript instances in shared objects. You do this by calling the flash.net.registerClassAlias() method to register the class. If you create an instance of your class and store it in the data member of your shared object and later read the object out, you will get a typed instance. By default, the SharedObject objectEncoding property supports AMF3 encoding, and unpacks your stored instance from the SharedObject object; the stored instance retains the same type you specified when you called the registerClassAlias() method.

您可以在共享对象中存储键入的ActionScript实例。您可以通过调用flash.net.registerClassAlias()方法来注册该类。如果您创建了类的实例并将其存储在共享对象的数据成员中,并稍后将该对象读出,则将获得一个类型化实例。默认情况下,SharedObject objectEncoding属性支持AMF3编码,并从SharedObject对象解压缩存储的实例;存储的实例保留您在调用registerClassAlias()方法时指定的相同类型。

One caveat is that storing object graphs can sometimes lead to storage issues. There is a limit to how much you can store in SharedObject before it notifies the user and asks for permission to store more. This threshold is 100k by default, I believe.

需要注意的是,存储对象图有时会导致存储问题。在通知用户并要求存储更多权限之前,您可以在SharedObject中存储多少内容。我相信这个阈值默认为100k。

#2


If you are using Flex Builder SDK or Flex Builder you can also use the [RemoteClass] metatag which will automatically register the class and make it serializable.

如果您使用的是Flex Builder SDK或Flex Builder,您还可以使用[RemoteClass]元标记,它将自动注册该类并使其可序列化。

#1


You can store any object in a SharedObject, but you need to register the class first:

您可以将任何对象存储在SharedObject中,但您需要首先注册该类:

You can store typed ActionScript instances in shared objects. You do this by calling the flash.net.registerClassAlias() method to register the class. If you create an instance of your class and store it in the data member of your shared object and later read the object out, you will get a typed instance. By default, the SharedObject objectEncoding property supports AMF3 encoding, and unpacks your stored instance from the SharedObject object; the stored instance retains the same type you specified when you called the registerClassAlias() method.

您可以在共享对象中存储键入的ActionScript实例。您可以通过调用flash.net.registerClassAlias()方法来注册该类。如果您创建了类的实例并将其存储在共享对象的数据成员中,并稍后将该对象读出,则将获得一个类型化实例。默认情况下,SharedObject objectEncoding属性支持AMF3编码,并从SharedObject对象解压缩存储的实例;存储的实例保留您在调用registerClassAlias()方法时指定的相同类型。

One caveat is that storing object graphs can sometimes lead to storage issues. There is a limit to how much you can store in SharedObject before it notifies the user and asks for permission to store more. This threshold is 100k by default, I believe.

需要注意的是,存储对象图有时会导致存储问题。在通知用户并要求存储更多权限之前,您可以在SharedObject中存储多少内容。我相信这个阈值默认为100k。

#2


If you are using Flex Builder SDK or Flex Builder you can also use the [RemoteClass] metatag which will automatically register the class and make it serializable.

如果您使用的是Flex Builder SDK或Flex Builder,您还可以使用[RemoteClass]元标记,它将自动注册该类并使其可序列化。