Page 1 of 1

Best way to send and receive data in Smartfox?

Posted: 04 Dec 2009, 15:36
by MoonChild
I'm working on the multiplayer for my Asteroids game. The idea of the multiplayer is that you and a someone else can play co-op. So togather you shoot on Asteroids, Aliens, etc.

What I got working so far is:
*Players sending and receiving each other's coordinates, rotation.
*Players sending and receiving each other's bullet coordinates, rotation,type
*Players sending and receiving the items they used(Shield, Speedup, etc)

And I'm currently working on the asteroids. Which are seperated into 3 arrays:
*big meteors
*medium meteors
*small meteors

So I started with the big meteors, and as it isn't working on the moment yet I'm already experiencing slow downs. And I know it's not related to my internet connection as I'm currently testing it locally.

Which made me wonder if I'm using the right way to send and receive data? I'm using the send to extension function. And what I send is a object with arrays.

So for example this would be placed in the Init_Meteors function:

Code: Select all

obj_example.x = new Array();
obj_example.y = new Array();

obj_example.x.push(a_player[playerID].xpos)
obj_example.y.push(a_player[playerID].ypos)
Then I have a send function that dispatches this object

Code: Select all

public function Send_BulletData():void
{
   dispatchEvent(new EventType("send.exampledata", false, false, obj_example));
}
This gets dispatched to another actionscript file which dispatches it again to another file that calls:

Code: Select all

smartfox.sendToExtension("ExampleExt", "send.exampledata", evt.arg[0]);
And in ExampleExt.as I have this:

Code: Select all

	//Send Example Data
	if (cmd == "send.exampledata")
	{
		var myZone = _server.getCurrentZone();
		var myRoom = myZone.getRoom(fromRoom);
		var playerID = user.getPlayerIndex();
		var usersInRoom = myRoom.getAllUsers();
		var usersToRespond = new Array();
		
		for (index = 0; index<usersInRoom.length; index++) {
			if (user != usersInRoom[index]) {
				usersToRespond.push( usersInRoom[index] );
			}
		}
		var response = {};
		response._cmd = "receive.exampledata";
		response.pID = playerID;
		response.pObject = params;
		//_server.sendResponse(response, fromRoom, null, usersToRespond);
		_server.sendResponse(response, fromRoom, null, myRoom.getAllUsers());
    }
So this sets some dispatch events in motion that will lead back to the actionscript file that has the Send_ExampleData function in it, where there is also a Receive_ExampleData
which looks something like this:

Code: Select all

public function Receive_ExampleData(exampledata:Object)
{
  for (var i:int = 0; i < MAX_EXAMPLES; i++)
  {
    a_player[i].xpos = exampledata.x;
    a_player[i].ypos = exampledata.y;
  }
}
So I'm wondering, am I doing this the right way or are there better ways to do this?