使用 writeObject() 发送数组

发布于 2024-12-22 17:47:56 字数 1997 浏览 3 评论 0原文

目前我正在使用 stage3D 开发一款回合制多人游戏。例如,当服务器发送一个对象时,下面的代码在客户端一切都非常顺利。

package models 
{
    public class Player 
    {
        public var type:String = "player";
        private var _action:String;
        private var _id:int;

        private var _username:String;
        private var _nickname:String;

        public function Player() 
        {

        }

        public function get id():int 
        {
            return _id;
        }

        public function set id(value:int):void 
        {
            _id = value;
        }

        public function get username():String 
        {
            return _username;
        }

        public function set username(value:String):void 
        {
            _username = value;
        }

        public function get nickname():String 
        {
            return _nickname;
        }

        public function set nickname(value:String):void 
        {
            _nickname = value;
        }

        public function get action():String 
        {
            return _action;
        }

        public function set action(value:String):void 
        {
            _action = value;
        }
        public function toString():String
        {
            return("id: " + id + " username: " + username + " nickname: " + nickname + "");
        }
    }

}

但是当我将一个数组放入对象中时,数组总是返回长度 0。现在我有点没有选择。我使用了 registerClassAlias

package models 
{
    /**
     * ...
     * @author RB
     */
    public class ListOfPlayers
    {
        public var type:String = "list_of_players";

        private var _list:Array = new Array();

        public function ListOfPlayers() 
        {

        }

        public function remove(i:int):void 
        {
            _list.slice(i);
        }

        public function add(player:Player):void 
        {
            _list.push(player);
        }

        public function get list():Array 
        {
            return _list;
        }

        }
    }
}

Currently I'm working on a turn based multiplayer game using stage3D. When the server sends a object for example the code below everything goes very smooth on the client side.

package models 
{
    public class Player 
    {
        public var type:String = "player";
        private var _action:String;
        private var _id:int;

        private var _username:String;
        private var _nickname:String;

        public function Player() 
        {

        }

        public function get id():int 
        {
            return _id;
        }

        public function set id(value:int):void 
        {
            _id = value;
        }

        public function get username():String 
        {
            return _username;
        }

        public function set username(value:String):void 
        {
            _username = value;
        }

        public function get nickname():String 
        {
            return _nickname;
        }

        public function set nickname(value:String):void 
        {
            _nickname = value;
        }

        public function get action():String 
        {
            return _action;
        }

        public function set action(value:String):void 
        {
            _action = value;
        }
        public function toString():String
        {
            return("id: " + id + " username: " + username + " nickname: " + nickname + "");
        }
    }

}

But when I put an array in the object the array always returns length 0. And now I'm a bit out of options. I used registerClassAlias

package models 
{
    /**
     * ...
     * @author RB
     */
    public class ListOfPlayers
    {
        public var type:String = "list_of_players";

        private var _list:Array = new Array();

        public function ListOfPlayers() 
        {

        }

        public function remove(i:int):void 
        {
            _list.slice(i);
        }

        public function add(player:Player):void 
        {
            _list.push(player);
        }

        public function get list():Array 
        {
            return _list;
        }

        }
    }
}

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

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

发布评论

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

评论(3

离去的眼神 2024-12-29 17:47:56

您是否尝试过为数组添加公共设置器?我认为这是你的问题

Have you tried adding public setter for the array? I think that's your problem

与君绝 2024-12-29 17:47:56

您可以发布在 SharedObject 中实际存储和检索 Player 类的代码吗?仔细检查 Flash Player 对 SO 的默认大小限制。 。 。它可能被关闭,或者太小(这对于个人用户来说是一个问题,所以你必须验证最终游戏的 FP 的 SO 大小设置)。

Can you post the code that actually stores and retrieves the Player class in a SharedObject? Double-check the Flash Player's default size limit for SOs . . . it may be turned off, or too small (this will be a problem for individual users, so you will have to verify FP's SO size setting for the final game).

勿忘初心 2024-12-29 17:47:56

简短的回答 - 您应该将数组声明为公共数组,或者实现 flash.utils.IExternalizable 接口(readExternal() 和 writeExternal() 方法)。

http://help.adobe.com/en_US /FlashPlatform/reference/actionscript/3/flash/utils/IExternalizable.html

IExternalized 接口提供对编码到数据流中的类的序列化的控制。 IExternalized 接口的 writeExternal() 和 readExternal() 方法由类实现,以允许自定义对象及其超类型的数据流的内容和格式(但不是类名或类型)。每个单独的类必须序列化并重建其实例的状态。这些方法必须与超类型对称才能保存其状态。这些方法取代了本机操作消息格式 (AMF) 序列化行为。

如果一个类没有实现,也没有继承自一个类
实现 IExternalized 接口,然后是一个实例
类将使用公共成员的默认机制进行序列化
仅有的。因此,类的私有成员、内部成员和受保护成员
将不可用。

Short answer - you should either declare your array as public, or implement the flash.utils.IExternalizable interface (readExternal() and writeExternal() methods).

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/IExternalizable.html

The IExternalizable interface provides control over serialization of a class as it is encoded into a data stream. The writeExternal() and readExternal() methods of the IExternalizable interface are implemented by a class to allow customization of the contents and format of the data stream (but not the classname or type) for an object and its supertypes. Each individual class must serialize and reconstruct the state of its instances. These methods must be symmetrical with the supertype to save its state. These methods supercede the native Action Message Format (AMF) serialization behavior.

If a class does not implement, nor inherits from a class which
implements, the IExternalizable interface, then an instance of the
class will be serialized using the default mechanism of public members
only. As a result, private, internal, and protected members of a class
will not be available.

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