
Example usage chart for Robin – the Flash / PHP multiuser system.
The white boxes are people using an application from their devices – each has their own color avatar. When the Orange one moves, it sends its properties to Robin. Robin then tells the others that there is new data. The others ask for the properties. All the properties (except for their own) get sent to them in an array (list). So moving would require x, and y (maybe z). They would ask for an array of x properties and an array of y properties. You can also get the last value sent (and with another command find the index of the sender).
If you choose an avatar (say that relates to an image on one of ten frames), then you would send that frame number when you move – since you are sending several properties now, use setProperties() like so, assuming you have a Robin object running called myRobin:
myRobin.setProperties({x:10, y:20, avatar:7});
Others recieve a DataEvent.DATA event and they can get everyone’s x, y and avatar properties from getProperties(“x”), getProperties(“y”) and getProperties(“avatar”). Each of these would return an array like this:
[22, 34, 10, 17] // array of x values
[44, 13, 20, 65] // array of y values
[1, 5, 7, 10] // array of avatar numbers
// if there are four others
Then you could loop through the array and position the avatar like so:
// make a private var players:Array = []; up top // make an Avatar MovieClip in library export as class in properties // Avatar clip has frames with different pictures // when a person first comes in they would choose theirs if (myRobin.numPeople > 0) { var xProp:String; // x of an avatar var yProp:String; // y of an avatar var aProp:String; // frame number of an avatar // there could be blanks in getProperties from people leaving // so always cycle through the full length of getProperties // instead of cycling through numPeople for (var i:uint=0; i<myRobin.getProperties("x").length; i++) { xProp = myRobin.getProperties("x")[i]; yProp = myRobin.getProperties("y")[i]; aProp = myRobin.getProperties("avatar")[i]; if (xProp != "") { // check for non blank if (!players[i]) { // check if we have not added avatar players[i] = new Avatar(); addChild(players[i]); } players[i].x = Number(xProp); players[i].y = Number(yProp); players[i].gotoAndStop(Number(aProp)); } else { if (players[i]) { // check to remove player removeChild(players[i]); players[i] = null; } } } } setChildIndex(myAvatar, numChildren-1);
Sorry if it looks complicated… beats a hundred lines…
Oh… if you want to assign a unique avatar to each new person, just set var num:Number = getProperties(“num”) + 1; and then setProperties(“num”) = num; so that other new folks have the right number. Then use your unique num property to set the Avatar clip to frame number num.
You should run this code in the DataEvent.DATA but also in a Timer every 500 ms or so to catch anybody leaving by closing the browser. This will get caught when people move but won’t get caught right away when people leave unless you do the timer. You also want to run this code when people start so the other players come in right away.
Okay… looks like I should make some sample code for this
Filed under: Tips Tagged: | actionscript, adobe, code, example, flash, multiuser, php










This is a good explanation.
The code you posted isn’t too hard to understand.
I got it completely.
It’ll be good for my game I’m wanting to create.
Right now, I’m just making the server and testing it.
I subscribed to this feed (RSS).
Hope you release more posts ;D
-Fighterlegend
I gave Robin a try, but I can’t seem to get Robin started on my server.
It tries to start it, but it can’t. In the PHP error_log it states:
[01-Feb-2010 14:57:19] PHP Warning: fsockopen() : unable to connect to 204.136.14.175:10505 (Connection refused) in /…/public_html/robin/runrobin.php on line 57
However, when working with socket_create, it works just perfect.
So, I might be a bit confused here, but does Robin actually CREATE a socket? Or does it use one I created myself?
Great work so far! Thanks for sharing this! Will look into the other classes as well later on
Hi Ronny – sorry for the delay in getting back – my e-mail that this goes to is clogged with some sort of virus the virus scan can’t seem to delete. Anyway…
Robin does create a socket in the Socket class in the classes folder. That code is relatively standard although there are some modifications to the traditional. We’ve had it going on a dozen or so different setups….
What server are you on? Also… did you update the mysetup.php to have your domain?
Please see: http://robinflash.wordpress.com/2010/04/06/unique-and-random-avatar-examples-with-robin-flash-php-open-source-multiuser-server/ for Avatar code!