简体   繁体   中英

AS3 AIR Correct way of sending data in multiplayer game

I am trying to make a multiplayer game, and I have successfully made a server and client program. I have made the client to send x and y position of the client hero by sending a string with "x,y". Are there any better way of sending data?

EDIT:

player is the player controlled sprite. player2 is the simulation of the other player.

Server code (for recieving):

private function inComingDataHandler(e:DatagramSocketDataEvent):void
    {
        textField.text = "Connected to: " + e.srcAddress;
        targetIP = e.srcAddress;
        var coordinates:Array = e.data.readUTFBytes(e.data.bytesAvailable).split(",");
        player2.x = Number(coordinates[0]);
        player2.y = Number(coordinates[1]);
        coordinates.splice(0, 1);
        coordinates.splice(1, 1);
    }

Client Code (for sending):

private function sendData():void
    {
        var data:ByteArray = new ByteArray();
        data.writeUTFBytes(String(player.x + "," + player.y));

        dataGramSocket.send(data, 0, 0, targetIP, targetPort);
    }

You may find it better and more organized to create a Model/Class of the data you will be sending back and forth. (especially if sending lots of data). Then serializing the entire model instance to a byte array.

//Create a model for the data that needs to be passed back and forth

package {
    import flash.utils.Dictionary;
    import flash.geom.Point;

    public class ServerData {
        public function ServerData():void {}

        public var coordinates:Point; //your coordinates
        public var someOtherVariable:MyCustomClass;
        public var someOtherVar:Dictionary;
    }
}

Then, serialize your model to a byte array:

private function sendData():void {
    var data:ServerData = new ServerData();  //make an instance of your class above
    data.coordinates = new Point(player.x,player.y);
    var ba:ByteArray = new ByteArray();
    ba.writeObject(data); //this serializes your object/model

    dataGramSocket.send(ba, 0, 0, targetIP, targetPort);
}

Then, reading it back in:

private function inComingDataHandler(e:DatagramSocketDataEvent):void {
    e.data.position = 0;
    var data:ServerData = e.data.readObject() as ServerData;

    player2.x = data.coordinates.x;
    player2.y = data.coordinates.y;

Lastly (and very importantly), you have to register any non-primitive classes (classes that require an import) that you plan on storing in the serialized byte array (this includes nested classes within other classes). In your constructor or anywhere really, do the following:

flash.net.registerClassAlias("flash.geom.Point", Point);
flash.net.registerClassAlias("com.myCustomClass", MyCustomClass);
flash.net.registerClassAlias("flash.utils.Dictionary", Dictionary);

If you're only sending that data, "x,y" is okay, but I like to be more descriptive. Something like "heroPosXY" will be more clear. If you are concerned about the number of characters, you can shorten that as far as "hpos".

Why these aren't very good:

  • "x,y" - this could be a 2-dimensional position for anything. not descriptive
  • "hp" - could be shorthand for "hero position", but one could think "hit points"

If your application will be sending more information of different types to, I suggest using AMF encoding. This will let you send entire objects at once, with decent compression built in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM