Activity 8 - Allowing the Enemies to Shoot

Now, we will make the enemies shoot back at the player. Each enemy will shoot randomly, and the difficulty (which will later be escaleted with each level) is defined by how often they randomly shoot.

The enemy beam is rather similar to the player’s beam. We will again make a very similar enemyBeam.js file with nearly the same code as the playerBeam.js but with different variable names and numbers:


We will share sprite and animation for the player’s beams and enemy’s beams. However, during implementation, you will create a separate group for enemy projectiles:


We also need to create a difficulty for this game (we will explain more about difficulty later, but for now, difficulty will make sure enemy will shoot):


  • We recommend setting difficulty to 1000.
  • If you set difficulty to 1000, the random number will pick from 1 to 1000. Only if the random number is 1, the enemy will shoot.
  • The enemy will shoot continously if you try difficulty to 1.

To make the enemies shoot randomly, we will first create an enemyShoot() method:


and call it in the update() method:


that creates a new enemy projectile at a specified random rate.

Then, we need to update enemy projectiles. We need to make sure each beam is deleted.

You will have to update enemy beams in enemyBeam.js like you did in playerBeam.js:

update_player_shoot again

You also need to update each enemy projectile in Scene 2, which is similar to how we update the projectiles:


In the end, it gameplay should look something like this: enemyShoot