
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 😉
Like this:
Like Loading...
Filed under: Tips | Tagged: actionscript, adobe, code, example, flash, multiuser, php | 4 Comments »