Skip to content

3. Shoot and Health

Series

You are reading Part 3 of the Space Shooter Tutorial.

  1. Space Shooter, Part 1
  2. Space Shooter, Part 2
  3. Space Shooter, Part 3
  4. Space Shooter, Part 4
  5. Space Shooter, Part 5
  6. Space Shooter, Part 6
  7. Space Shooter, Part 7
  8. Space Shooter, Part 8
  9. Space Shooter, Part 9
  10. Space Shooter, Part 10
  11. Space Shooter, Part 11
  12. Space Shooter, Part 12
  13. Space Shooter, Part 13

Tip

In this chapter, we will learn how to use Behaviours in our game. You can read the Behaviors page to learn more about them. Behaviors add extra capabilities to objects. All objects can have one or more behaviors attached to it.

Install health and fire behaviors

Select the three dots next to the Player object and click Edit object. Then, click the Behaviors tab.

Click on the Add a new behavior to the object and select Search new behaviors tab. Now you can type "Health" on the search bar to find our behavior. Select the Health behavior and install it. By doing that, we will be able to use this behavior in the future without downloading it again.

Repeat the same step to install Fire Bullets behavior.

Add health and fire capability to the player

On the player's edit view, click on Add a new behavior to the object and search "fire" on Installed Behaviors. Then, select Fire Bullets. We'll now be able to fire bullets using an action that we'll add in the next steps.

Set cooldown to 0.3.

Repeat the same process to add Health ability to the player. Change Maximum health from 100 to 0, since we do not want to limit our player's maximum health.

Tip

In the next chapters, we will add some powerups to increase the player's health.

Shooting bullets from the player spaceship

Firstly, we need to add a bullet object. Follow the steps in the previous episode and add a sprite object. Use the name Bullet and add "laser_player.png" image for the object.

We will add a new behavior, called Destroy when outside of the screen, to the Bullet object, because we want to delete the objects that are not on the screen.

Tip

The Destroy when outside of the screen behavior allows us to prevent shooting the outside of the camera by deleting objects outside of the screen.

Add "Destroy when outside of the screen" behavior to the Bullet object.

You can see your new Bullet object under the Player object.

Then, open the Events page. Add a new event. Click on add a condition and type "key" to check whether the Space key is pressed or not.

Now, we will add the shooting action. Click on add action in the same event, and select Player. Then, on the top, you can see the new actions coming from our behaviors. Select Fire a bullet and fill out the requirements like below:

Player.X() is an expression that will be replaced by the position on the player's X-axis. We add 50 to position the bullet at the right of the object. The same goes for the Y-axis. The other parameters indicate that we'll create a "Bullet" object and gives it some speed. The angle is 0 degrees, meaning the bullets will go on the right.

Making the player a bit smaller

The player spaceship is a bit too big. We could resize it in the scene editor, but for this tutorial we'll use events. Click on Add a new action to change the scale of the player. Then, select the Player and Scale. Fill the value with 0.6.

Warning

Make sure that modification sign is = (set to).

You can add a "Player" comment to separate the actions. You events page should look like this:

Testing our shoot ability

Save and run the game. Try pressing Space to see how you can shoot.

Tip

We will add some health actions later, after creating our enemies.

Next step

In the next chapter, we'll add a background and make the camera move: Space Shooter, Part 4