Skip to content

Instantly share code, notes, and snippets.

@dpogorzelski
Created March 1, 2014 22:32
Show Gist options
  • Save dpogorzelski/9298517 to your computer and use it in GitHub Desktop.
Save dpogorzelski/9298517 to your computer and use it in GitHub Desktop.
function create() {
game.stage.backgroundColor = '#2d2d2d';
player = game.add.sprite(0, 0, 'char'); //spawning our player
player.animations.add('down', [0, 1, 2], 10); //adding animations
player.animations.add('left', [12, 13, 14], 10);
player.animations.add('right', [24, 25, 26], 10);
player.animations.add('up', [36, 37, 38], 10);
player.body.collideWorldBounds = true;
//create a new ws connection and send our position to others
sock = new WebSocket("ws://" + ip + ":3000/ws");
sock.onopen = function() {
var pos = JSON.stringify({
x: player.x,
y: player.y
});
sock.send(pos);
};
// when we receive a message we spawn, destroy or update a player's position
// depending on the message's content
sock.onmessage = function(message) {
var m = JSON.parse(message.data);
if (m.New) {
players[m.Id] = spawn(m);
} else if (m.Online === false) {
players[m.Id].label.destroy();
players[m.Id].destroy();
} else {
uPosition(m);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment