Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created June 9, 2020 17:13
Show Gist options
  • Save Srushtika/2040bdfd606bb1d5d3acd2cd10e9cc15 to your computer and use it in GitHub Desktop.
Save Srushtika/2040bdfd606bb1d5d3acd2cd10e9cc15 to your computer and use it in GitHub Desktop.
Code snippet 15 - For multiplayer space invaders article
function subscribeToPlayerInput(channelInstance, playerId) {
channelInstance.subscribe("pos", (msg) => {
if (msg.data.keyPressed == "left") {
if (players[playerId].x - 20 < 20) {
players[playerId].x = 20;
} else {
players[playerId].x -= 20;
}
} else if (msg.data.keyPressed == "right") {
if (players[playerId].x + 20 > 1380) {
players[playerId].x = 1380;
} else {
players[playerId].x += 20;
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment