Create Enemies

(Step 3/9) Learn how to add enenies into your game.

1. Go into the preload function in game.js and add a new image named after the new character set.


code/client/src/game.js
g.loadImage('enemy',  'enemy.png');



2. In the room.js file we need to put a setupCharacters function in the onInit function.


code/server/rooms/room.js
g.setupCharacters('enemy');

3. In the game.js file we need to put an addCharacters function in the init function.


code/client/src/game.js
g.addCharacters("enemy", .5)

4. In game.js, Add a getCharacters function in the create function.

code/client/src/game.js
g.getCharacters("enemy")

5. In the room.js file in the onInit function. We’re going to put a createACharacter function in a for loop.


code/server/rooms/room.js
let i;
	for  (i =  0; i <  15; i++)  { g.createACharacter('enemy', g.nextCharacterId('enemy'),  { x: Math.floor((Math.random()  *  500)  +  1), y: Math.floor((Math.random()  *  1900)  +  1)  })  }

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


Add a Background
Add Enemy Movement

Need Help?

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

Click Here for Help