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.

g.loadImage('enemy',  'enemy.png');

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


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

g.addCharacters("enemy", .5)

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


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

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, 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