Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

Camp Imagination Videogame Design Arena Shooter: Enemies

Creating Enemies

This lesson picks up where the Arena Shooter: Shooting lesson left off. The first thing we have to do is create a sprite and object for the enemy.

Health and Speed Variables
  1. Right-click on Sprites in the resource tree.
  2. Choose "Create" to open the Sprite Editor.
  3. Name your sprite "sprEnemy."
  4. Click on "Import" to select the image you want to use. If you are using the default image it is named "enemy" in the "TutorialResources" folder.
  5. Set the origin to the "Middle Center."
  6. Right-click on Objects in the resource tree.
  7. Choose "Create" to open the Object Editor.
  8. Name your object "objEnemy."
  9. Click on "No Sprite" and select the "sprEnemy" sprite.
  10. Add a Create event to enemy object.
  11. Add an Assign Variable action from the "Common" section.
  12. Call the variable "enemyHealth" and set the value to "5."
  13. Add another Assign Variable action.
  14. Call the variable "enemySpeed" and set the value to "1.5."

We added two variable to the enemy to track its health and speed. We will use the enemySpeed variable whenever we move the enemy. It is useful to have a variable for the speed in case we want to change it later. You can give the enemy more or less health if you want to make the game harder or easier.

If Instance Exists Action

Chasing the Player

  1. Add a Step event to the enemy object.
  2. Add the If Instance Exists action from the "Instance" section.
  3. Change the object to "objPlayer."
  4. Add the Set Point Direction action to the right of the previous action.
  5. Set "X" to "objPlayer.x" and "Y" to "objPlayer.Y."
  6. Add the Set Speed action below the previous action.
  7. Set the speed to "enemySpeed.
  8. Add the Set Instance Rotation action outside the if.
  9. Set the angle to "direction."

The actions above will cause an enemy to follow the player by moving toward the player's x and y location. The enemy will rotate based on the direction it is moving and the speed is the "enemySpeed" variable that was set earlier. Try adding some enemies to the "Instances" layer of your room to see how this works.

Collision

Right now, nothing happens when the player shoots the enemies or when the enemies touch the player. Let's change that.

  1. Add an If Variable action to the Step event of the enemy.
  2. Change the variable to "enemyHealth" and check if it is less than or equal to 0.
  3. Add the Destroy Instance action to the right of the previous action.
Apply To Action

This code will destroy the enemy if it has lost all of its health. We now need to make the health go down when a projecticle hits the enemy.

  1. In the projectile object, add a Collision event with "objEnemy."
  2. Add the Apply To action from the "Common" section.
  3. Click on the right-most arrow of the Apply To action and choose "Other."
  4. Drag the Assign Variable action under the previous action.
  5. Assign the "enemyHealth" variable a relative value of "-1."
  6. Drag the Destroy Instance action to the bottom of your actions.

You may notice that the projectiles disappear a little early, but they are clearly hitting the enemies. We can fix this by changing the collision mask of our sprites.

Collision Mask
  1. Open the projectile sprite.
  2. Click on the section called "Collision Mask."
  3. Change the mode to "Manual."
  4. Drag the collision mask box to form a small box at the front of the image.
  5. Modify the collision masks of the player and enemy to make them slightly smaller than the sprites.
  6. Open the player object and add a Collision event with the enemy.
  7. Add a Restart Game action from the "Game" section.

The game will now restart when an enemy collides with the player and the collisions look a lot better.