Avatar Chat example checking if user has changed position

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

Post Reply
ceteri
Posts: 8
Joined: 12 Jan 2012, 23:42

Avatar Chat example checking if user has changed position

Post by ceteri »

I am just going through the avatar chat example and am struggling to understand why the conditions in the if statement indicate that a user has changed position.

why would changedVars.indexOf(USERVAR_X) be equal to -1 if the variables had not changed?

code from the website:

// Check if the user changed his position
if (changedVars.indexOf(USERVAR_X) != -1 || changedVars.indexOf(USERVAR_Y) != -1)
{
// Check if avatar exists
if (getAvatar(user.id) != null)
{
// Move the user avatar
moveAvatar(user)
}
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Post by rjgtav »

Hi.

In order to save bandwith and server resources, when you update an user's variables, only those variables are sent to the server and the other clients. Not the whole list!
When you receive the USER_VARIABES_UPDATE event, it comes with those changedVars parameters, which is an Array containing the name of the variables that were updated. So, by checking if there is or not that variable in the changedVars list, you know if the variable was updated or not.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Post Reply