SharedObject 支持哪些数据类型?
我知道用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将任何对象存储在 SharedObject 中,但你需要先注册课程:
需要注意的是,存储对象图有时会导致存储问题。 在 SharedObject 通知用户并请求存储更多数据之前,您可以在 SharedObject 中存储的数据量是有限制的。 我相信这个阈值默认是 100k。
You can store any object in a SharedObject, but you need to register the class first:
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.
如果您使用的是 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.