Connections Help

Learn how to use connections between your server and your game.

1. Sending actions to server

First, we are setting this up so that when someone uses the keyboard or mouse everyone else playing your game will know as well. To do this we will need to send the action to a server that is running everyones game. This is done with the sendAction function on the click method in the game.js file.

click(x, y) {
	g.sendAction('click', { x, y });

This allows you to send the spot that the person clicked in the game to everyone else playing the game.

2. Receiving the action

Now we need to receive the message in the room.js file. In the room.js file we need to get all of the actions and tell your game what to do when they receive an action. To do this we will get them all in the onMessage method.

onMessage(client, data) {
	const  actions = {
		click: () =>  g.createACharacter('zombie', g.nextCharacterId('zombie'), {x:data.x, y:data.y}),

This would create a character at the spot that they clicked when anyone clicks.

Then, in the room.js file we need to put the handleActions function to run all of your actions when an action occurs. We put this in the onMessage method under our actions that we received.

onMessage(client,data) {
	g.handleActions(actions, data);

Need Help?

Proofread your code, check with your team and classmates, and if you’re still stuck!

Join us on Google Meet