对象数组的 SharedObject。重启flash时无法获取正确数据
我有一个数组,存储了一些对象及其数据,我尝试将其存储在我的计算机中。 如果我在保存数据后尝试加载数据,我可以获得正确的数据。 Exp: [Object Player]
但是如果我重新启动闪存,数据似乎消失了。
问题是什么?
private var sharedObject:SharedObject = SharedObject.getLocal("aquarium", "/");
public function save(n:String):void
{
/* player list will only handle the list of all the Players
* each player data will handle by Player class itself.
*/
registerClassAlias("Player", Player)
player = new Player()
player.newPlayer(n, LATEST_VERSION)
playerArray.push(player)
//saving as shared object
sharedObject.data.aquariumData = playerArray
sharedObject.flush()
load()
}
public function load():void
{
if (sharedObject.size > 0)
{
trace("loading player info")
playerArray = sharedObject.data.aquariumData
trace(playerArray)
}
else
{
trace("there's no record")
}
}
I have an array that stored some Object with its data, and I try to store it in my computer.
If I try to load for the data after I've save the data, I could get a correct data.
Exp: [Object Player]
But if I restart the flash, the data seems to be gone.
What is the problem?
private var sharedObject:SharedObject = SharedObject.getLocal("aquarium", "/");
public function save(n:String):void
{
/* player list will only handle the list of all the Players
* each player data will handle by Player class itself.
*/
registerClassAlias("Player", Player)
player = new Player()
player.newPlayer(n, LATEST_VERSION)
playerArray.push(player)
//saving as shared object
sharedObject.data.aquariumData = playerArray
sharedObject.flush()
load()
}
public function load():void
{
if (sharedObject.size > 0)
{
trace("loading player info")
playerArray = sharedObject.data.aquariumData
trace(playerArray)
}
else
{
trace("there's no record")
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能提供一下如何获取共享对象的代码吗?
您使用 var sharedObject:SharedObject = SharedObject.getLocal("sharedObject"); 或类似的东西吗?
除此之外,在序列化之前调用
registerClassAlias("Player", Player)
时,请记住,也必须在提取数据之前调用它,这样反序列化才能正确工作并返回 Player 数组对象不是 Object 对象的数组。刷新后关闭共享对象是非常好的做法:)
PS据我测试,您的代码可以用其他自定义类替换您的 Player 类。
Can you please provide the code how you obtain the shared object ?
Do you use
var sharedObject:SharedObject = SharedObject.getLocal("sharedObject");
or something like this ?Apart from that when calling
registerClassAlias("Player", Player)
before serialization keep in mind that it must be called before extraction of the data also, so the de-serialization will work correctly and returns array of Player objects not array of Object objects.And ofc closing the sharedObject is very nice practice after flushing :)
P.S. Your code works as far as i've tested it replacing your Player class with other custom class.