Reputation: 2628
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);
}
Upvotes: 0
Views: 170
Reputation: 14406
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);
Upvotes: 2
Reputation: 5087
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:
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.
Upvotes: 1