The_dude8080 Posted December 20, 2016 Share Posted December 20, 2016 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 Link to comment Share on other sites More sharing options...
squilibob Posted December 21, 2016 Share Posted December 21, 2016 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. Link to comment Share on other sites More sharing options...
The_dude8080 Posted December 21, 2016 Author Share Posted December 21, 2016 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? Link to comment Share on other sites More sharing options...
squilibob Posted December 27, 2016 Share Posted December 27, 2016 That is why there is two listeners, one to get only a new player's details and one to get every user The_dude8080 1 Link to comment Share on other sites More sharing options...
Recommended Posts