对象数组的 SharedObject。重启flash时无法获取正确数据

发布于 2024-12-06 21:15:54 字数 1031 浏览 0 评论 0原文

我有一个数组,存储了一些对象及其数据,我尝试将其存储在我的计算机中。 如果我在保存数据后尝试加载数据,我可以获得正确的数据。 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 技术交流群。

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

发布评论

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

评论(1

笑,眼淚并存 2024-12-13 21:15:54

您能提供一下如何获取共享对象的代码吗?
您使用 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.

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