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: Shooting

Creating Projectiles

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

Arena Shooter Projectile
  1. Right-click on Sprites in the resource tree.
  2. Choose "Create" to open the Sprite Editor.
  3. Name your sprite "sprProjecticle."
  4. Click on "Import" to select the image you want to use. If you are using the default image it is named "spr_bullet" in the "TutorialResources" folder.
  5. Set the origin of the sprite toward the front of the projectile by clicking on the sprite image.
  6. Right-click on Objects in the resource tree.
  7. Choose "Create" to open the Object Editor.
  8. Name your object "objProjectile."
  9. Click on "No Sprite" and select the "sprProjectile" sprite.
Create Instance Action

Before we can make the projectile move, we need it to appear when the player clicks the mouse button.

  1. Double-click the "objPlayer" object.
  2. Double-click the Step event.
  3. Add the If Mouse Down action from the "Mouse & Keyboard" section.
  4. Underneath the If Mouse Down action, add the Create Instance action from the "Instances" section.
  5. Select the "objProjectile" object in the "Object" section.
  6. Type "x" for the "X" section and "y" for the "Y" section.

The projectiles should now appear underneath the player when you press the left mouse button. The "x" and "y" you typed referred to the x and y location of the player when the mouse button was clicked, which is why the projectiles appear underneath the player.

The projectile is placed on a layer called "Instances." A layer is a 2D space where object can be stored within a room. Layers can be reordered some some layers appear above or below other layers.

Moving Toward the Cursor

Set Point Direction Action

Right now the projectiles don't move, but we want them to start moving toward the cursor once they are fired.

  1. Double-click the "objProjectile" object.
  2. Add a Create event.
  3. Add the Set Point Direction action from the "Movement" section.
  4. Set "X" to "mouse_x" and "Y" to "mouse_y."
  5. Add the Set Instance Rotation action from the "Instances" section.
  6. Set the rotation to "direction."
  7. Add the Set Speed action from the "Movement" section.
  8. Set the speed to "16."

The projectiles will now fire toward the mouse cursor and face the direction the player is facing, which is also toward the mouse cursor. Each projectile will move 16 pixels every frame.

Cooldown Period

The projectiles work great, but they fire a bit too fast. The game would be way too easy if you could fire this often. To fix this, we will need to add a "cooldown period" so you can't continuosly fire projectiles.

If Variable Action
  1. Double-click the "objPlayer" object.
  2. Add a Create event.
  3. Add a Assign Variable action.
  4. Name the variable "cooldown."

We only want to be able to fire when the cooldown variable is 0.

  1. Open the Step event of "objPlayer."
  2. Add an If Variable action between "If Mouse Down" and "Create Instance."
  3. Change the variable to "cooldown," set "Is" to "Less, and the value to "1."
  4. Add another Assign Variable action underneath "Create Instance.
  5. Change the name of the variable to "cooldown" and the value to "3."
  6. Add another Assign Variable action outside of the previous if statements.
  7. Set the "cooldown" variable to a value of "-1" and check the "Relative" box.

The above actions will allow a projectile to fire if the cooldown is less than 1. When a projectile is fired the cooldown is set to 3. The cooldown is constantly counting down by 1 (relative means it changes based on the current value). If you want the cooldown to be shorter or longer, you can change the 3.