SharedObject 支持哪些数据类型?

发布于 2024-07-26 18:56:24 字数 1027 浏览 6 评论 0原文

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

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

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

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

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

stageOne = sharedObject.data.stageOne;

我收到此错误:

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

我想我的问题是:到底是什么样的数据类型可以存储在 SharedObject 中吗? 我在网上查到的所有地方都用“任何可以在 Flash 中使用的东西”来回答这个问题,这并不是很具有描述性——显然我的 GameStage 类也可以在 Flash 中使用。 是否存在我不知道的从 SharedObject 检索数据的情况?

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

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:

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:

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-08-02 18:56:24

您可以将任何对象存储在 SharedObject 中,但你需要先注册课程:

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

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

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

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.

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.

﹎☆浅夏丿初晴 2024-08-02 18:56:24

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文