Code Warrior 4

Planning
Step 1
This lesson will focus on shooting projectiles from our enemy

The projectiles will be images of errors and warnings

4
Step 2
Start by downloading the new images and adding it to the Assets list

Click the blue link below to download the compressed folder with our images

Download enemyProjectiles.zip

After EXTRACTING the images, drag and drop both images to our assets list

5

Refer to the start of the 3rd lesson if you need help adding images

Step 3
Make sure you add the image variables to load the asset images inside images.js

Refer to the start of the 3rd lesson if you need help

9
Information
Step 4
We will start by making the strafer projectile which will shoot straight down

It needs to be placed centered with the x position and underneath the enemy

6
Step 5
Update the enemy's shoot function to spawn a strafer projectile
7
Check Your Work
Step 6
Save your code

We should see this weird spray of projectiles, but at least they started underneath the enemy!

Information
Step 7
We will need to add types to our projectiles, similar to how we did different enemy types
8
Step 8
Pass in an argument to our projectile constructor so we know the type
10
Challenge
Step 9
Let's also add a random image for the projectile using the new images

If you need help, then refer to how we received the random symbol

11
Step 10
Let's also add if statements to our projectile's draw function to draw each type
12
Step 11
Also add if statements to the projectile's move function to move each type
13
Step 12
To test this code, we need to pass in the projectile type for the player and enemy

Inside player.js:

14

Inside enemy.js:

15
Check Your Work
Step 13
Save your code

The enemy projectiles should be images moving down (bomber enemies will look weird because they move at the same speed down) and the player projectiles should be normal.

Information
Step 14
The bomber enemy will shoot two projectiles on both sides
17
Step 15
Start by changing the enemy's shoot function to check the enemy types
19
Step 16
Now add code to spawn two projectiles on either side of the bomber

The blank code should place the projectile on the right side of the enemy

18
Step 17
Let's update projectile's draw function to also draw an image for the bomber enemy
20
Step 18
Let's also update the projectile's move function to move the bomber's projectile in the x axis
21
Check Your Work
Step 19
Save your code

The bomber's projectiles should be flying sideways correctly!

Information
Step 20
It may look like a cool pattern, but we will want the enemy to space it's shooting using a timer
22
Step 21
Start by adding some variables we will use to time our shooting and moving

Add these variables inside the enemy's constructor function:

23

Note the shoot rate is 60 because there is 60 frames per second by default

Step 22
Let's now add a "checkShoot" function in our enemy's class

This will toggle the moving every second and shoot when it first stops

24
Step 23
Let's replace calling the shoot function with our new checkShoot function
25
Challenge
Step 24
Lastly, add an if statement to the enemy's move function to check if it can move

Hint: We need to check IF the enemy CAN'T MOVE to return early

26
Check Your Work
Step 25
Save your code

We should see a one second delay between moving and shooting

Challenge
Step 26
How can we shorten the delay?

Hint: I made it wait 20 frames instead of 60 in the video below

This lesson is now complete! The next lesson will focus on COLLISION!

Code Warrior 4 Info

Account

MVCode Clubs

Created By

Missing tiny aaronjau101

Course:

Code Warrior

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach