Setup Bullets

Learn how to make a super cool zombie game

1. in game.js, Create bullets using the addCharacters() function inside the init() function.
code/client/src/game.js

	g.addCharacters('bullets', 0.5);


2. in game.js, Create loadImage() function to load the image for the bullets.
code/client/src/game.js

	g.loadImage('bullets', 'bullet.png');


3. In game.js, Create a getCharacters() function to add the bullets to the game in the create() function.
code/client/src/game.js

	g.getCharacters('bullets');


4. In room.js, Create a setupCharacters() function inside the onInit() function to set them up on the server.
code/server/rooms/room.js

	g.setupCharacters('bullets');


5. In room.js, Create onMessage()method to get our bullets to shoot.
code/server/rooms/room.js

			click: () => {

		},


6. In room.js, in the onMessage() function in the click method that you just created Add code to make the bullets animate and go in the right direction.
code/server/rooms/room.js

			const index = g.nextCharacterId('bullets');
			g.createACharacter('bullets', index, { x: player.x, y: player.y });
			let newCharacter = g.getACharacter('bullets', index);
			g.playAnimation(newCharacter, 'x',
				g.getXTowards(newCharacter, data.x, data.y) * 500, 2000);  
			g.playAnimation(newCharacter, 'y',
				g.getYTowards(newCharacter, data.x, data.y) * 500, 2000);
			setTimeout(() => g.deleteACharacter('bullets', newCharacter.id), 2000);


7. In game.js, Make sure your Click Function looks like this.

Now there is only two more things we need to do to make our bullets shoot, first check and make sure your game.js file has a click method that looks like this:

code/client/src/game.js

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


8. In room.js, add the handleAnimations() function in the onUpdate() function for our bullets.
code/server/rooms/room.js

	g.handleAnimations('bullets');


9. In room.js, Add code to handle the collision and delete the zombies if they collide with the bullets in the onUpdate() function.
code/server/rooms/room.js

	g.handleCollision('bullets', 'zombies', (bullet, zombie) => {
		g.deleteACharacter('zombies', zombie.id);
		g.deleteACharacter('bullets', bullet.id);
	});

Now you should have working bullets that can kill the zombies when you shoot!


Download your zip, and upload it to blobbert.io, and you should have a background!


Add a Health Bar
Setup Login Screen

Need Help?

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

Click Here for Help