Camp Imagination Videogame Design Arena Shooter: 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.
- Right-click on Sprites in the resource tree.
- Choose "Create" to open the Sprite Editor.
- Name your sprite "sprEnemy."
- 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.
- Set the origin to the "Middle Center."
- Right-click on Objects in the resource tree.
- Choose "Create" to open the Object Editor.
- Name your object "objEnemy."
- Click on "No Sprite" and select the "sprEnemy" sprite.
- Add a Create event to enemy object.
- Add an Assign Variable action from the "Common" section.
- Call the variable "enemyHealth" and set the value to "5."
- Add another Assign Variable action.
- 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.
- Add a Step event to the enemy object.
- Add the If Instance Exists action from the "Instance" section.
- Change the object to "objPlayer."
- Add the Set Point Direction action to the right of the previous action.
- Set "X" to "objPlayer.x" and "Y" to "objPlayer.Y."
- Add the Set Speed action below the previous action.
- Set the speed to "enemySpeed.
- Add the Set Instance Rotation action outside the if.
- 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.
Right now, nothing happens when the player shoots the enemies or when the enemies touch the player. Let's change that.
- Add an If Variable action to the Step event of the enemy.
- Change the variable to "enemyHealth" and check if it is less than or equal to 0.
- Add the Destroy Instance action to the right of the previous 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.
- In the projectile object, add a Collision event with "objEnemy."
- Add the Apply To action from the "Common" section.
- Click on the right-most arrow of the Apply To action and choose "Other."
- Drag the Assign Variable action under the previous action.
- Assign the "enemyHealth" variable a relative value of "-1."
- 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.
- Open the projectile sprite.
- Click on the section called "Collision Mask."
- Change the mode to "Manual."
- Drag the collision mask box to form a small box at the front of the image.
- Modify the collision masks of the player and enemy to make them slightly smaller than the sprites.
- Open the player object and add a Collision event with the enemy.
- 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.