Add Scoring A Goal

Learn how to create an awesome online soccer game!

1. In room.js, Add data to the players.
code/client/src/game.js

	g.createACharacter('players', client.sessionId,
		{ ...data, x, y, score: 0, lives: 3, block3s: 0, block5s: 0 });

This data sets your players lives that they start with, and blocks that they start with.


2. In room.js, Add the kicker.
code/client/src/game.js

		kicker: '',


3. In room.js, Add this code to know who kicked the ball last.
code/client/src/game.js

		ball.kicker = player.id;


4. In room.js, Add this code so we can update the lives and scores when a goal is made.
code/client/src/game.js

		g.handleCollision('goals', 'soccerBalls', (goal, ball) => {
		if (ball.kicker !== goal.id) {
			g.getACharacter('players', ball.kicker).score += 1;
			g.getACharacter('players', goal.id).lives -= 1;
			g.deleteACharacter('soccerBalls', ball.id);
			setTimeout(() => this.addABall(), 3000);
		}
	});


5. In game.js Add a check so we know if the game is over.
code/client/src/game.js

},
	() => {},
	(id, attr, value) => {
		if (id === g.myId() && attr === 'lives' && value <= 0) {
			location.reload();
		}


Download your zip, and upload it to blobbert.io, and you should be able to score goals!


Adding Kicking
Add Blocks

Need Help?

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

Click Here for Help