Created
June 9, 2020 17:13
-
-
Save Srushtika/2040bdfd606bb1d5d3acd2cd10e9cc15 to your computer and use it in GitHub Desktop.
Code snippet 15 - For multiplayer space invaders article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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