Hi,
In my game I have extended the scene class and the aim is to create a multiplayer game so I am running my express server which emits an event to the individual clients when it has paired that player with another player. Then I want to call a function which will create the player. I am wondering if it is possible or the only way is to put the code directly into the event for the socket?
The socket event (inside the create function) looks like :
var paired = false;
this.socket.on('pair', function (pair) {
if(!paired) {
alert(JSON.stringify(pair));
this.createPlayer(pair);
}
});
Then the create player function looks like:
createPlayer: function (player) {
if(player.playerNo == 1) {
// create the player sprite
else {
//create the player 2 sprite
}
},
If this isn’t possible and I have to create the players from within the socket event, then how do I scope player1 and player2 to the scene class? Do I have to have something like:
this.player1;
this.player2;
var player1 = this.player1;
var player2 = this.player2;
And then set the player 1 and 2 that I create to the variables? What is the correct way to scope the objects you want available to the whole scene?