Sign in to follow this  
Followers 0
The_dude8080

Phaser + sockets. Doubts.

4 posts in this topic

Greetings. I am trying to create a multiplayer game with Phaser. I need some orientation though.

I was wondering how do you create a list, which can probably be an array or a js object that stores, in the client side, the connected players. I would need that in several situations => for example, when a new player connects I would check that list and create a new players according to the information stored there. I am not sure how can I create such a variable that would keep all the players info because when a new player connects I think that the variable is generated again which means the previous players information will not be contained there.

Also, about the physics, how would you take care of collisions? Should I implement normal collisions in the phaser update loop and then with collision callback send the data to server and take care of it from there? And for example phaser physics gravity? How should I take care of that in a multiplayer game

Share this post


Link to post
Share on other sites

You could store the users as an object and have a socket listener check whenever the server sends a new user to the listener like:

var userlist = {} 
if (socket.hasListeners('new user') == false) {
  socket.on('new user', function(newuser) {
    if (!userlist[newuser.name]) userlist[newuser.name] = newuser;
  });
  socket.emit('ask for all users');
}

if (socket.hasListeners('all users') == false) {
  socket.on('all users', function(payload) {
    userlist = payload;
  });
}

Have your server keep track of the co-ordinates of all the players so that the latency of the player's connections does not give any players a disadvantage. When a client moves their player you send the new velocities to the server. Periodically sending out everyone's co-ordinates from the server should be fine for a small scale game and should hopefully prevent your players experiencing jittering enemy player movement.

Share this post


Link to post
Share on other sites
4 hours ago, squilibob said:

You could store the users as an object and have a socket listener check whenever the server sends a new user to the listener like:


var userlist = {} 
if (socket.hasListeners('new user') == false) {
  socket.on('new user', function(newuser) {
    if (!userlist[newuser.name]) userlist[newuser.name] = newuser;
  });
  socket.emit('ask for all users');
}

if (socket.hasListeners('all users') == false) {
  socket.on('all users', function(payload) {
    userlist = payload;
  });
}

Have your server keep track of the co-ordinates of all the players so that the latency of the player's connections does not give any players a disadvantage. When a client moves their player you send the new velocities to the server. Periodically sending out everyone's co-ordinates from the server should be fine for a small scale game and should hopefully prevent your players experiencing jittering enemy player movement.

But the object userlist, when a new player connects, won't it only contain the new player? All the other players previously connected will be in that "userlist" since a new variable is spawned?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.