XMLSocket : Live game

Hey all, me and some other buds are creating a game where we need to have multiple players in ‘real time’. I have tested the solution of using a database/text file to handle the player positions, but it used huge amounts of CPU w/ only 2 players. So I wrote a socket server in PHP, and now I need help creating the AS part of it :slight_smile:

I already wrote a bunch of accessors, I would like you to tell me if this is the best way to do it, as I am a total AS n00b.

/* Handle the receiving of player positions */
XMLSocket.prototype.onData = function (PlayerList)
{
	var i = 0;
	var CurrentUsername = "";
	var PlayerArray = PlayerList.split("|");
	for(Container in PlayerArray)
	{
		if (i == 0)
		{
			CurrentUsername = Container;
			i = i + 1;
		} 
		if (i == 1)
		{
			//assign coordinates to player with CurrentUsername;
			i = 0;
			CurrentUsername = "";
		} 
	} 
} 

/* Initialize socket connection */
_global.InitSocket = function(Username)
{
	var IP = "xxx.xxx.xxx.xxx";	   // IP address of socket server
	var Port = "xxxx";				// Port of socket server to listen on
	_global.socket = new XMLSocket(); // Create socket object
	_global.socket.connect(IP, Port); // Connect to socket
	_global.socket.send(Username);	// Send username
	_global.socket.send("0,0");	   // Initial position
} 

/* Close socket connection */
_global.CloseSocket = function()
{
	_global.socket.close();
} 

/* Send new player position to socket */
_global.SendPosition = function(Position)
{
	_global.socket.send(Position);
} 
InitSocket("PLAYERUSERNAME");

Well, all and all, it seems quite well thought out.
Only thing you should make a habit of is strict typing of variables:

/* Handle the receiving of player positions */
XMLSocket.prototype.onData = function (playersList) {
	var playersArray:Array = playersList.split("|");
	for(var i=0; i<playerArrays.length; i++) {
			var currentUsername = playersArray;
			// Not quite sure what your code was doing here, but here's my grasp of it...
			with (CurrentUsername) {
				//assign coordinates to player with CurrentUsername.
			}
		} 
	} 
}

As you can see, I’ve also changed your for loop to a more conventional way.
The reason for this is because the for … in loop goes from the “end” of the array, to the begninning, while this one goes from 1 - up, and is a proper to use a loop with an array. the for … in is usually used for objects that you have no way of telling how many objects there are, or what’s their identifiers are.

[size=1]A note of Variable Typing: When you type variables it enables the flash compiler to find and identify errors better. In your specific case it doesn’t make that much of a difference, but in more complex applications this makes a world of difference![/size]
[size=1]* Also, typing enables the AS panel to provide you with relative drop-downs in some cases, which speeds up coding.[/size]